我有一个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
}
}
});
网址会返回以下echo json_encode($result);
$result
的值,如果成功则可以成功,如果不成功则可以是其他任何内容。
PHP以此echo $_GET['callback']."(".json_encode($result).");";
答案 0 :(得分:0)
只有在您正在访问的Web服务设置为跨域访问时,您才可以请求并获取jsonp,因此您的ajax调用必须正确且Web服务必须正确。
ajax call
$.ajax({
type: "GET",
cache: false,
dataType: 'jsonp',
// we are setting base_url at the top, like http://www.MyDomain.com/MyPage.svc/
url: base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject()),
contentType: "text/plain",
success: function (theJson) {
// I make sure I got json
if (theJson.indexOf('{') > -1 ) {
glb_the_quote = $.parseJSON(theJson);
if (glb_the_quote.errorMessage.length == 0) {
PopulateResultsPage();
} else {
alert('There was an error getting the quote: ' + glb_the_quote.errorMessage);
}
} else {
alert(theJson)
}
},
error: function (req, status, error) {
if(status == "timeout"){
ShowNoInternetConnectionWarning();
} else {
alert("There was an internet error = " + status + ", " + error);
}
},
// this gives the webservice 7 seconds to return
timeout: 7000
});
// end ajax;
现在的网络服务:有一点似乎我必须在与网络服务代码相同的目录中正确配置网络配置 - .svc文件 - 这就是我所做的。
这就是我在svc文件中添加的内容:
<%@ ServiceHost Language="C#" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Debug="true" Service="gfeWebService.ws.wsGfe" CodeBehind="wsGfe.svc.cs" %>
webconfig必须包含以下内容(注意crossDomainScriptAccessEnabled =“true”)
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<!-- the names have to be fully qualified. If you get an error that says, I can't find blah blah, you don't have the names right -->
<services>
<service name="gfeWebService.ws.wsGfe">
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP"
contract="gfeWebService.ws.IwsGfe"
behaviorConfiguration="webHttpBehavior"
>
</endpoint>
</service>
</services>
</system.serviceModel>
提示
在url:line附近的js代码中放置一个断点,抓住最终在url中的值:...换句话说,抓住这个结果如何解析
base_url +“GetGfeQuote?strJsonRequestObject =”+ JSON.stringify(LoadedGetQuoteObject())
并将其粘贴到浏览器的地址栏中。您可以通过这种方式获得更有意义的错误消息。
HTH
答案 1 :(得分:0)
你可以使用YQL
绕过CORS,只要你只做GET请求,而不是使用会话或任何棘手的事情。
$.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) {
// ...
}
);