Answer the question
In order to leave comments, you need to log in
Cross Domain XmlHttpRequest
Good afternoon friends.
I've been struggling with this issue all day today. Initial task:
When running on site.com, send a POST request (even a response is not required) to xxx.site.com. As a last resort, I am ready to limit myself to the Google Chrome browser.
It would seem that the task is no longer new, a lot of materials have been written on the topic, but something does not work ...
So, what I have already tried:
var iframe = $( '<iframe src="xxx.site.com" style="display:none;"></iframe>' ).appendTo( document .body);
var iDoc = iframe.get(0).contentDocument;
varscript = iDoc.createElement( "script" );
script.setAttribute( "type" , "text/javascript" );
script.innerHTML =
"function postIt() {" +
"var xhr = new XMLHttpRequest();" +
"xhr.onreadystatechange = function(data) {" +
"alert(xhr.readyState);" +
"if (xhr.readyState == 4) {" +
" if (xhr.status == 200) {" +
"alert(data.toString());" +
" } else {" +
" alert(xhr.error);" +
"}" +
"}};" +
"var url = \"xxx.site.com\";" +
"var postParams = \"a=1&b=1\"" +
"xhr.open(\"POST\", url, true);" +
"xhr.setRequestHeader(\"Content-Type\", \"application/x-www-form-urlencoded;\");" +
"xhr.send(postParams);" +
"}" +
"postIt();" ;
iDoc.body.appendChild(script);
* This source code was highlighted with Source Code Highlighter .
Answer the question
In order to leave comments, you need to log in
So do you need an answer or not?
If there is no task to get an answer, then:
In someone else's iframe, no one will write js according to the security policy.
To solve the problem, I can advise you to redo the server logic of xxx.site.com so that GET is perceived as well as POST, and use JSONP.
The second option can be used if the xxx.site.com logic is not corrected: make a resource on your host to which to send POST, and make an HTTP request from the site.com server to the xxx.site.com server.
You can also try to make a hidden form, set its action to " xxx.site.com ", add inputs with the desired name and value, and fire the form submit. Not sure if this will work everywhere.
Not suitable, unfortunately, because. xxx.site.com checks Origin, and the browser puts site.com in Origin
I didn’t do it myself, but everything works fine on hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ (I look in Firefox 4 beta 12, Chrom is not at hand). There are examples that you can "feel". + article ( developer.mozilla.org/en/HTTP_access_control ) on MDN (MDC) which should help resolve the issue.
Try the methods from this comment habrahabr.ru/blogs/webdev/114432/#comment_3692960
Probably already studied, but still - javascript.ru/ajax/cross-domain-scripting .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question