Display result of POST or GET to external site within jQuery dialog
I'm trying to POST to an external website, and display the results in a jQuery UI Dialog on my site. Is that possible? I've tried a bunch of permutations, for example (with GET):
$("#view").click(function() {
var url = this.href;
var dialog_pop = $('<div></div>');
dialog_pop.load(url).dialog();
return false; });
This seems to work if the target URL is within my domain, but doesn't work开发者_高级运维 if it's an external site. Also, I haven't gotten POST to work yet either.
What can I try to solve this?
You can't do this with a normal XmlHttpRequest, which jQuery AJAX uses. This restriction is part of the same-origin policy that browsers enforce or security reasons.
What you can do is use JSONP if the other domain supports transferring data in that fashion. It's basically a specialized way of passing JSON and calling a function that exists on your side.
You can send a POST to your domain like this:
$.post(url, data, function(result) {
$('<div>' + data + '</div>').dialog();
});
However, as has already been mentioned, you cannot send a normal AJAX request to a different domain.
精彩评论