{"id":8132,"date":"2015-11-28T01:39:46","date_gmt":"2015-11-28T01:39:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/28\/how-do-i-send-a-cross-domain-post-request-via-javascript-open-source-projects-request-request\/"},"modified":"2022-08-30T15:03:00","modified_gmt":"2022-08-30T15:03:00","slug":"how-do-i-send-a-cross-domain-post-request-via-javascript-open-source-projects-request-request","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/28\/how-do-i-send-a-cross-domain-post-request-via-javascript-open-source-projects-request-request\/","title":{"rendered":"How do I send a cross-domain POST request via JavaScript?-open source projects request\/request"},"content":{"rendered":"<p>If you control the remote server, you should probably use CORS, as described in the top answer on this question; it&#8217;s supported in IE8 and up, and all recent versions of FF, GC, and Safari. (But in IE8 and 9, CORS won&#8217;t allow you to send cookies in the request.)<\/p>\n<p>So, if you <em>don&#8217;t<\/em> control the remote server, or if you have to support IE7, or if you need cookies and you have to support IE8\/9, you&#8217;ll probably want to use an iframe technique.<\/p>\n<ol>\n<li>Create an iframe with a unique name. (iframes use a global namespace for the entire browser, so pick a name that no other website will use.)<\/li>\n<li>Construct a form with hidden inputs, targeting the iframe.<\/li>\n<li>Submit the form.<\/li>\n<\/ol>\n<p>Here&#8217;s sample code; I tested it on IE6, IE7, IE8, IE9, FF4, GC11, S5.<\/p>\n<pre><code>function crossDomainPost() {\n  \/\/ Add the iframe with a unique name\n  var iframe = document.createElement(\"iframe\");\n  var uniqueString = \"CHANGE_THIS_TO_SOME_UNIQUE_STRING\";\n  document.body.appendChild(iframe);\n  iframe.style.display = \"none\";\n  iframe.contentWindow.name = uniqueString;\n\n  \/\/ construct a form with hidden inputs, targeting the iframe\n  var form = document.createElement(\"form\");\n  form.target = uniqueString;\n  form.action = \"http:\/\/INSERT_YOUR_URL_HERE\";\n  form.method = \"POST\";\n\n  \/\/ repeat for each parameter\n  var input = document.createElement(\"input\");\n  input.type = \"hidden\";\n  input.name = \"INSERT_YOUR_PARAMETER_NAME_HERE\";\n  input.value = \"INSERT_YOUR_PARAMETER_VALUE_HERE\";\n  form.appendChild(input);\n\n  document.body.appendChild(form);\n  form.submit();\n}\n<\/code><\/pre>\n<p>Beware! You won&#8217;t be able to directly read the response of the POST, since the iframe exists on a separate domain. Frames aren&#8217;t allowed to communicate with each other from different domains; this is the same-origin policy.<\/p>\n<p>If you control the remote server but you can&#8217;t use CORS (e.g. because you&#8217;re on IE8\/IE9 and you need to use cookies), there are ways to work around the same-origin policy, for example by using <code>window.postMessage<\/code> and\/or one of a number of libraries allowing you to send cross-domain cross-frame messages in older browsers:<\/p>\n<ul>\n<li>Porthole<\/li>\n<li>XSSInterface<\/li>\n<li>EasyXDM<\/li>\n<li>jQuery PostMessage Plugin<\/li>\n<\/ul>\n<p>If you don&#8217;t control the remote server, then you can&#8217;t read the response of the POST, period. It would cause security problems otherwise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you control the remote server, you should probably use CORS, as described in the top answer on this question; it&#8217;s supported in IE8 and up, and all recent versions of FF, GC, and Safari. (But in IE8 and 9, CORS won&#8217;t allow you to send cookies in the request.) So, if you don&#8217;t control [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8132","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8132"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8132\/revisions"}],"predecessor-version":[{"id":8595,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8132\/revisions\/8595"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}