-1

I know it's possible to load any kind of document from any domain from JavaScript (without necessarily being able to peek at its content), but it usually concerns regular GET requests. What about POST?

Is it possible to make an HTTP POST request from JavaScript to any domain name? (I'm specifically interested in form submissions.)

If so, how?

cnst
  • 25,870
  • 6
  • 90
  • 122
  • *"I know it's possible to load any kind of document from any domain from JavaScript..."* It is??? – the system Mar 15 '13 at 18:00
  • You can load any document in place of existing one: http://stackoverflow.com/questions/2383401/javascript-setting-window-location-href-versus-window-location . Also, for elements like images or iframes, I think you can change `src` to any location, too. (Well, maybe other than referencing files on the local filesystem of the user.) – cnst Mar 15 '13 at 18:06
  • You need to set Acecss-Control-Allow-Origin response headers on the target server. At least for IE9. – Jani Hyytiäinen Mar 15 '13 at 18:08
  • @cnst: That question addresses requesting a new page. Of course you can do that. But that's not directly making a GET or POST request. That's just telling the browswer to take you elsewhere on the internet, and it uses a GET request to accomplish that. You need to clarify your meaning in your question. If you're talking about loading from within JavaScript, it implies that you want to fetch any document to use within your program, and so no, you can't just do that in a GET or POST using any arbitrary document from any arbitrary domain. – the system Mar 15 '13 at 18:11
  • i've clarified the question. interested in making the request itself, and not looking at the returned content – cnst Mar 15 '13 at 18:24

1 Answers1

0

As per some answers on a nearby question, «HTTP GET request in JavaScript?», you might use XMLHttpRequest, since, according to the docs, the POST method is supported, too.

http://www.w3.org/TR/XMLHttpRequest/
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest

A sample code from the above w3.org document:

function log(message) {
  var client = new XMLHttpRequest();
  client.open("POST", "/log");
  client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
  client.send(message);
}

However, it would seem like in order for it to work with POST requests to domains unrelated to yours (where instead of "/log", a complete http or https URL is specified), the Cross-Origin Resource Sharing may have to be supported and enabled on the target server, as per https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Simple_requests.

So, it seems like, at least through XMLHttpRequest, you cannot make form submissions through POST requests (in fact, looks like even GET requests won't fly, either).

Community
  • 1
  • 1
cnst
  • 25,870
  • 6
  • 90
  • 122
  • What about the Same Origin Policy? – the system Mar 15 '13 at 18:02
  • The destination would need to allow communication from a different domain using CORS. – Bafsky Mar 15 '13 at 18:04
  • Same Origin Policy is not violated here. Your JavaScript cannot get into executing in the context of another domain, but there is nothing that would prevent it from referencing content from another domain. https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript#Cross-origin_network_access explicitly mentions, "*Cross-origin writes are typically allowed. Examples are links, redirects and form sumissions. Certain rarely used HTTP requests require preflight.*" – cnst Mar 15 '13 at 18:14
  • Your answer isn't a link, redirect or form... it's an XHR request. And *referencing content* from a different domain is certainly not automatically allowed, without agreement from the server using headers. – the system Mar 15 '13 at 18:22
  • So, I can't make a form submission to an unrelated domain from JavaScript, without express agreement of such domain? – cnst Mar 15 '13 at 18:29
  • @cnst: Are you talking about a form submission *(as in an HTML form)*, or an XHR request? – the system Mar 15 '13 at 19:00
  • yeap, just the HTML form emulation with a POST request through JavaScript, don't really need XHR. I think it's definitely possible through JS, but looks like not exactly through XHR. – cnst Mar 15 '13 at 19:46