使ajax调用跨域 [英] Make ajax call cross domain
本文介绍了使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'){
//显示谢谢你记得在
$里添加一个对话框contactpage.find('。contact-thankyou' ).show();
$ contactpage.find('。contact-form')。hide();
} else {
alert('无法发送您的信息,请重试。'); //记得在
}中添加一个对话框}
}
});
url返回以下 echo json_encode($ result);
$ result
的值可以是成功的,如果成功则为其他任何值。
PHP以此 echo $ _GET ['callback']。(。json_encode($ result)。);;
您可以使用 YQL
绕过CORS,只要您只执行GET请求,使用会话或任何棘手的问题。
$ .getJSON(http://query.yahooapis.com/v1/public/yql ?+
q = select%20 *%20from%20html%20 where%20url%3D%22+ encodeURIComponent(base_url +GetGfeQuote?strJsonRequestObject =+ JSON.stringify(LoadedGetQuoteObject b $ b%22& format = xml'& callback =?,
function(theJson){
// ...
}
);
I have an ajax call I would like to become cross domain how can I do this? The script is below
$.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
}
}
});
The url returns the following echo json_encode($result);
the value of $result
can be success if successful and anything else if unsuccessful.
the PHP ends with this echo $_GET['callback']."(".json_encode($result).");";
解决方案
You can use YQL
to get around CORS, as long as you're only doing GET requests, and not using sessions or anything tricky.
$.getJSON("http://query.yahooapis.com/v1/public/yql?" +
"q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent( base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject())) +
"%22&format=xml'&callback=?",
function (theJson) {
// ...
}
);
这篇关于使ajax调用跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文