{"id":48,"date":"2023-09-10T22:06:22","date_gmt":"2023-09-10T16:36:22","guid":{"rendered":"https:\/\/successrouter.com\/tech\/?p=48"},"modified":"2023-09-10T22:06:22","modified_gmt":"2023-09-10T16:36:22","slug":"jquery-common-place-to-modify-all-the-ajax-request","status":"publish","type":"post","link":"https:\/\/successrouter.com\/tech\/jquery-common-place-to-modify-all-the-ajax-request\/","title":{"rendered":"Jquery: Common place to modify all the ajax request"},"content":{"rendered":"\n<p>A lot of time we have to add some common parameter in request header or need to include some parameter in request body or query string. Jquery gives us the flexibility to the same using ajaxSetup method<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$.ajaxSetup({\n\t\/\/ Define a global before request send\n\tbeforeSend: function(xhr, settings) {\n\t\tif (settings.contentType) {\n\t\t\t\/\/Update the query string to append useridentifier\n\t\t\tsettings.data = (settings.data || \"\") + (settings.data ? \"&amp;\" : \"\") + \"useridentifier=\"+window.userIdentifier;\n\t\t\t\n\t\t\t\/\/set the header Content-Type\n\t\t\txhr.setRequestHeader(\"Content-type\", settings.contentType);\n\t\t}\n\t\treturn true;\n\t}\n});\n<\/pre>\n\n\n\n<p>The above piece of code will append few things with every request body and add the request header with that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of time we have to add some common parameter in request header or need to include some parameter in request body or query string. Jquery gives us the flexibility to the same using ajaxSetup method $.ajaxSetup({ \/\/ Define a global before request send beforeSend: function(xhr, settings) { if (settings.contentType) { \/\/Update the query&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[14,13],"_links":{"self":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/posts\/48\/revisions\/49"}],"wp:attachment":[{"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/successrouter.com\/tech\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}