仅当您访问的 Web 服务设置为跨域访问时,您才能请求并获取 jsonp,因此您的 ajax 调用必须正确且 Web 服务必须正确。
ajax 调用
$.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;
现在是 web 服务:在某一时刻,我似乎必须在与 web 服务代码相同的目录中正确配置一个 web 配置 - .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>
提示
并将其粘贴到浏览器的地址框中。这样你会得到更有意义的错误信息。
- 在您处理此问题时让 Fiddler 运行,并检查发送和接收的内容。
高温高压