开发者

Display result of POST or GET to external site within jQuery dialog

开发者 https://www.devze.com 2023-01-12 08:10 出处:网络
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):

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号