在装载功能$ .ajaxSetup全球数据犯规合并功能、数据、全球、ajaxSetup

2023-09-10 20:05:28 作者:懒得热情

我想设置一个全局数据参数,为我所有的Ajax请求

I would like to set a global data parameter for all my ajax requests

$.ajaxSetup({
   data: {hash : "12345"}
});

设置此我呼吁后:

after setting this I call:

var myData = {
    name : "John",
    age : "28"
}

$.get(url, myData, function(data){
    ...
});

这工作正常,并添加所有3个参数(哈希,姓名,年龄),要求数据 但是当我打电话负载功能而不是GET,它不工作,我得到的只有2个参数(姓名,年龄):

this works fine and add all 3 parameters (hash, name, age) to request data but when I call load function instead of get, it doesnt work and I get only 2 parameters (name, age):

$("#my_div").load(url, myData, function(data){
    ...
});

请谁能告诉我,为什么它没有负载功能的工作?我有负载功能的许多用途在我的应用程序,我不想改变负载上的get

please could anyone tell me why it doesnt work for load function? I have many usages of load function in my app and I dont want to change load on get

感谢您为每一个提示!

推荐答案

这也算是在jQuery的一个bug;或者至少是,它们应该接受接口之间的AJAX其方法是不一致的。

This could be considered a bug in jQuery; or at the very least, they should accept the interface between their AJAX methods are inconsistent.

解决这个问题的唯一方法是使用 jQuery.extend 合并默认数据数据您提供:

The only way to fix this is by using jQuery.extend to merge the default data with the data you've provided:

jQuery.extend(myData, jQuery.ajaxSettings);

在提出请求。

如何了一个错误:

负荷 转换中的数据对象转换为字符串的之前传递到底层的jQuery阿贾克斯的方法,其中的 GET 不的。

load converts the data object into a string before passing in onto the underlying jQuery.ajax method, where as get doesn't.

由于这个原因,当 ajaxExtend 构建数据对象,在负载方案中的数据参数被设置为字符串,而用 GET 数据对象的合并的与 jQuery.ajaxSettings

Because of this, when ajaxExtend builds the data object, in the load scenario the data parameter is set to the string, whereas with get the data object is merged with jQuery.ajaxSettings.