jQuery的ajaxSetup - 我想补充的默认数据仅供GET请求 [英] jQuery's ajaxSetup - I would like to add default data for GET requests only
问题描述
在一个Ajax驱动的网站,我已经添加使用ajaxSetup一些默认数据,鼻翼这样的:
变种版本='159';
$ .ajaxSetup({
数据类型:文字JSON
的contentType:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8,
数据: {
R:修订
}
});
这是为了保证高速缓存未命中时,一个新版本部署和前端要求从后端HTML模板或JSON数据。后端和前端份额为此相同的版本号。
问题是,后端有关获取参数'r',此前端做了PUT,POST或DELETE不是有点不高兴。难道就没有办法告诉jQuery的AJAX这个数据应该只在执行GET请求时,并没有做POST时使用,PUT和DELETE请求。
更新:
我试过beforeSend函数首先,因为我知道这一点。然而改变settings.data是可能的,但任何变化似乎消失时beforeSend返回。这可能是我的错...: - )
我已经看中了阿贾克斯preFilter代替。这是不容易,因为馅饼虽然。该options.data不是一个对象,而是$ .PARAM(对象)的结果,所以第一个挑战是要取消其参数化。我结束了这一点:
变种版本='159';
$就prefilter(功能(选项,originalOptions,jqXHR){
//不发送数据POST / PUT / DELETE
如果(originalOptions.type!=='GET'|| options.type!=='GET'){
返回;
}
VAR数据= originalOptions.data;
如果(originalOptions.data!== undefined)的{
如果(Object.prototype.toString.call(originalOptions.data)=== [对象字符串]'){
数据= $ .deparam(originalOptions.data); //看到http://benalman.com/$c$c/projects/jquery-bbq/examples/deparam/
}
} 其他 {
数据= {};
}
options.data = $ .PARAM($扩展(数据,{R:修订}));
});
开始的jQuery 1.5,你可以更优雅通过的 prefilters 的:
变种版本='159';
$就prefilter(功能(选项,originalOptions,jqXHR){
//不发送数据POST / PUT / DELETE
如果(originalOptions.type!=='GET'|| options.type!=='GET'){
返回;
}
options.data = $ .extend(originalOptions.data,{R:修订});
});
In a ajax-driven site I have added some default data using the ajaxSetup, ala this:
var revision = '159';
$.ajaxSetup({
dataType: "text json",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: {
r: revision
}
});
This is to ensure cache-miss when a new revision is deployed and the frontend ask for html templates or json-data from the backend. The backend and frontend share the same revision number for this reason.
The problem is that the backend it somewhat unhappy about getting the parameter 'r' when the frontend does a PUT, POST or DELETE. Is there no way to tell jQuery's ajax that this data should only be used when doing GET requests and not when doing POST, PUT or DELETE requests.
UPDATE:
I tried the beforeSend function first, since I knew it. However changing settings.data was possible, but any change seemed to vanish when beforeSend returned. It may have been my fault... :-)
I have settled on the ajaxPreFilter instead. It was not easy as pie though. The options.data is not an object, but the result of $.param(object), so the first challenge was to un-parameterize it. I ended up with this:
var revision = '159';
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
// do not send data for POST/PUT/DELETE
if (originalOptions.type !== 'GET' || options.type !== 'GET') {
return;
}
var data = originalOptions.data;
if (originalOptions.data !== undefined) {
if (Object.prototype.toString.call(originalOptions.data) === '[object String]') {
data = $.deparam(originalOptions.data); // see http://benalman.com/code/projects/jquery-bbq/examples/deparam/
}
} else {
data = {};
}
options.data = $.param($.extend(data, { r: revision }));
});
Starting jQuery 1.5, you can handle this much more elegantly via Prefilters:
var revision = '159';
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
// do not send data for POST/PUT/DELETE
if(originalOptions.type !== 'GET' || options.type !== 'GET') {
return;
}
options.data = $.extend(originalOptions.data, { r: revision });
});
这篇关于jQuery的ajaxSetup - 我想补充的默认数据仅供GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!