如何完全重置自定义AJAX对象

How do I fully reset a custom AJAX object?

本文关键字:AJAX 对象 自定义 何完全      更新时间:2023-09-26

我已经通过一个新对象传递了AJAX参数,然后我将这个对象传递给jQuery的$.ajax()。我遇到的问题是试图完全重置我的对象。我的意思是,我知道有一个简单优雅的方法,我只是没有看到它。事实上,我认为现在,我根本无法完成它。谁能帮帮忙?

这是我正在做的一个片段。

    var app{};
    var async = {};
    app.resetAjax = function(){
            app.async = {
                url: 'this/path/',
                dataType = 'JSON';
                type = 'GET';
            return app.async; //important, because i want to return it, this way. 
        }   
    };  
    //Then, come in later and set it up in another file
    async = app.resetAjax();
    async.type = 'POST';
    async.dataType = 'XML';
    async.headers : { ///defaults
        "SomethingWayWierd" : "hopeless", 
    };
    async.data = app.someOtherProp;
    async.timeout= 3000; //maybe one more tweak
    //then run the call somewhere
    $.ajax(async); //Now, this call MUST be XML, and the POST, not the default 'JSON' and 'GET'

所以,注意,我的重置函数,resetAjax();。这是我的问题。我该如何正确地重置它(即删除我的重置功能并用建议的更简单的东西替换它)。会不会像

这么简单
app.async = new $.ajax();

或者

app.async = $.ajax();

这些对我不起作用,它们正在失败,就像我最初在原始函数中所做的那样。

所以什么是正确的方式来确保没有以前的设置被传递到我的自定义变量称为async,在下一轮,在SPA?

我甚至试过这个:

    app.async = {
        accepts: null,  //default: depends on DataType) Type: PlainObject
        async: true, //boolean, true by default, set to false if you want to send synchronously
        beforeSend: null, //Type: Function( jqXHR jqXHR, PlainObject settings )
        cache: null, // (default: true, false for dataType 'script' and 'jsonp')
        complete: null, //Type: Function( jqXHR jqXHR, String textStatus )
        contents: null,  //Type: PlainObject
        contentType: null, // (default: 'application/x-www-form-urlencoded; charset=UTF-8')
        context: null, //Type: PlainObject
        converters: null, // (default: {"* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML})
        crossDomain: null, //(default: false for same-domain requests, true for cross-domain requests)   Type: Boolean
        data: null, //{} //object, string ,array
        dataFilter: null, //Type: Function( String data, String type ) => Anything
        dataType: null, //'json', //'json', // string, what type of data do we want the server to send back?  there is no default on this, we can add one here if we need. JSON BROKE IT< DONT USE JSON AS DEFAULT
        error: null, //Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ),  DEFINED in app.router. rsvpAjax
        global: true, //boolean, defaults to true, set to false for prevent global handlers like ike ajaxStart or ajaxStop from being triggeredl
        headers: null, //(default: {}) Type: PlainObject
        ifModified: false, //boolean, defaults to false, if set to true then allow request to be successful only if the response has changed since last request
        isLocal: null, //(default: depends on current location protocol)  Type: Boolean
        jsonp: null, //Type: String
        jsonpCallback: null, //Type: String or Function()
        method: null,  //  (default: 'GET') Type: String
        mimeType: null,  //Type: String, A mime type to override the XHR mime type.
        password: null,  //Type: String
        processData: null,  //  (default: true) Type: Boolean //set to false if you want to send the form as a dom element
        scriptCharset: null,  //Type: String, Sets the charset attribute on the script tag, only applies when the "script" transport is used (e.g., cross-domain requests with "jsonp" or "script" dataType and "GET" type).
        statusCode: null,  // (default: {})  Type: PlainObject
        success: null,  // Type: Function( Anything data, String textStatus, jqXHR jqXHR ), DEFINED in app.router. rsvpAjax
        timeout : 10000, //10000 is 10 seconds //number,  app.router.ajax.timeout = null; //number,
        traditional: null,  // Type: Boolean, Set this to true if you wish to use the traditional style of param serialization.
        type: 'GET', // (default: 'GET') Type: string,  the type of request to make
        url: null, //  (default: The current page)  Type: String
        username: null,  //Type: String
        xhr: null,   //(default: ActiveXObject when available (IE), the XMLHttpRequest otherwise) Type: Function().  This is the callback for creating the XMLHttpRequest object.
        xhrFields: null  //Type: PlainObject, An object of fieldName-fieldValue pairs to set on the native XHR object. For example, you can use it to set withCredentials to true for cross-domain requests if needed.
    };

和可能会继续得到一个涓滴错误的效果,它开始说'accept:'不能是'null',直到我得到所有的设置拨号正确,并解释,我不想保持。所以我觉得我需要ajax.reset()之类的东西。

代码中有一个正在进行的提升问题,并且是一个全局问题。如果asyncapp的属性,那么您可以缩小范围。之后,使用它作为属性,重置它,然后微调它,然后调用ajax。

<?php 
var app{};
app.async = {};
//add your defaults here
app.resetAjax = function(){
        app.async = {
            url: 'default/path',
            dataType = 'JSON',
            type = 'GET', 
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8';
    }   
};  
//in your module adjust it when needed, then call it
app.resetAjax();
app.async.type = 'POST';
app.async.dataType = 'XML';
app.async.headers : { ///defaults
    "SomeWierdHeader" : "its value", 
};
app.async.data = app.someOtherProp;
app.async.timeout= 3000; //maybe one more tweak
//run it
$.ajax(app.async);