Submitting post data via jquery .ajax to another domain
For the life of me I cannot get this to work. I get the alert back when the test.php file i开发者_开发技巧s on the same server as this application, but if I point it to another domain, it does not work at all. The function is:
$.ajax({
url: 'http://www.mycrossdomain.co.uk/test.php',
crossDomain: true,
dataType: 'json',
success: function(data) {
alert('success');
}
});
Does anybody have any useful printers?
That is same origin policy at work.
You can't post (that is, the HTTP POST request method with key value pairs as body) to a different domain, protocol and/or port via XHR.
You could play around with CORS but know it is not supported in < IE8.
You can, however, submit a form to a different URL.
The only way you can make this work is to create a "proxy" page on your server where the JS lives that will relay the data you send via the JSON request to the third party server and will return the results directly to your script. The problem here (as is pointed out in the comments below) is that while you can get a JSONP request to work to another domain, you cannot send the data via a POST request; this is because all a "JSONP request" does is insert a script tag into your DOM, so the only option you have is passing params via GET via the script src url.
精彩评论