我有一个ajax调用我想成为跨域我该怎么做?该脚本如下让ajax调用跨域
$.ajax({
type: "GET",
url: url,
data: {sendername: sendername, email: email, subject: subject, message: message},
dataType: "jsonp",
crossDomain: "true",
success: function (data) {
if (data == 'success') {
// show thank you remember to add a dialog inside
$contactpage.find('.contact-thankyou').show();
$contactpage.find('.contact-form').hide();
} else {
alert('Unable to send your message. Please try again.'); //remember to add a dialog inside
}
}
});
的URL返回以下echo json_encode($result);
的$result
值可以成功,如果成功,其他任何东西如果不成功。
PHP的结尾这个echo $_GET['callback']."(".json_encode($result).");";
如果在主机上没有设置CORS头,设置'crossDomain:true'将不会奇迹般地工作。无论如何,这应该是自动检测的。 – 2013-03-26 02:01:12
[jQuery ajax跨域]的可能重复(http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – Nix 2013-03-26 02:02:45
@AndrewMao确定不知道CORS头是什么,但我有许多其他脚本工作跨域,唯一的区别是我可以看到的是,有一个数据类型的回调和jsonp数据类型 – 2013-03-26 02:05:21