views:

187

answers:

2

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.

A: 

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

RaYell
I think the documentation is unclear here. For a POST request, `data` should be converted to a query string and sent via the POST body.
Andy E
+2  A: 

$.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" }
Andy E