javascript - Does jQuery ajaxSetup method not work with $.get or $.post? - Stack Overflow

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

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

For example, I might have this code:

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

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

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

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

{
  persist: true,
  name: "foo"
}

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.

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

For example, I might have this code:

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

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

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

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

{
  persist: true,
  name: "foo"
}

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.

Share Improve this question edited Jun 26, 2015 at 17:26 Sumurai8 20.8k11 gold badges69 silver badges102 bronze badges asked Mar 30, 2010 at 4:55 Justin PolieyJustin Poliey 16.3k7 gold badges39 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

$.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 }

with

{ name: "foo" }


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).

As documentation says data option is converted to query string and appended to the URL for GET requests.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346947a4623594.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信