我有一個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