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.
2 Answers
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条)