请问jQuery的ajaxSetup方法不能含有$不用彷徨或$。员额工作?员额、彷徨、方法、工作

2023-09-10 19:24:03 作者:明月照山河

请问jQuery的 $。ajaxSetup 方法不尊重数据的选项散场时, $。交 $。获得被称为?

Does the jQuery $.ajaxSetup method not respect the data field in the options hash when $.post or $.get is called?

例如,我可能有这样的code:

For example, I might have this code:

$.ajaxSetup({ data: { persist: true } });

然后,发送POST请求,我会叫这样的:

Then, to send a POST request, I would call this:

$.post("/create/something", { name: "foo" });

我期待实际的POST数据看起来是这样的:

I was expecting the actual POST data to look like this:

{
  persist: true,
  name: "foo"
}

$发送的唯一的数据后 {名富} 。有什么办法来获得预期的行为?我使用jQuery 1.4.1。

but the only data sent by $.post is { name: "foo" }. Is there any way to get the expected behavior? I'm using jQuery 1.4.1.

推荐答案

$。ajaxSetup()默认设置为你的Ajax请求。您在请求方法设置的任何选项将覆盖这些默认值,而不是将它们合并。你实际上覆盖

$.ajaxSetup() sets defaults for your ajax requests. Any options you set in the request method will override these defaults, not merge them. You're actually overriding

{ persist: true }

{ name: "foo" }

这似乎不再是这样的— 上jQuery的跟踪的表明,在其中加入在一个版本更新和jQuery现在合并对象的车票而不是替换默认的(感谢@Quincy)。

This no longer appears to be the case — a ticket on the jQuery tracker suggests that this was added in a version update and jQuery now merges the objects instead of replacing the default (thanks @Quincy).