Does jQuery ajaxSetup method not work with $ .get or $ .post?

Does the jQuery $.ajaxSetup method $.ajaxSetup the data field in parameter hashes when calling $.post or $.get ?

For example, I might have this code:

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

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

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

I expected the actual POST data to look like this:

 { persist: true, name: "foo" } 

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

+4
source share
2 answers

$.ajaxSetup() sets the default values ​​for your ajax requests. Any parameters specified in the query method will override these default values, and not combine them. You really redefine

 { persist: true } 

from

 { name: "foo" } 

<h / "> This is no longer the case; a ticket to the jQuery tracker suggests that it has been added to the version update, and jQuery now merges objects instead of the default replacement (thanks @Quincy).

+6
source

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

0
source

Source: https://habr.com/ru/post/1305510/


All Articles