我已经通过一个新对象传递了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();
这些对我来说不起作用,它们失败了,就像我最初在原始函数中所做的那样。
那么确保以前没有设置传递到我的自定义变量var中的正确方法是正确的方法,在下一轮,在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.
};
并且可能会继续得到一个涓涓细流的错误效果,它开始说“接受:&#39;在我得到正确拨打的所有设置并且考虑到我不想维护的设置之前,我就不会“无效”。所以,我觉得我需要ajax.reset()
或其他什么。
答案 0 :(得分:1)
在代码中存在一个悬挂问题,以及一个全局问题。如果async
是app
的属性,则缩小范围。稍后,使用它作为属性,重置它,然后微调它,然后调用你的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);