ヒットしている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サービスコードと同じディレクトリ(.svcファイル)にWeb構成を正しく構成する必要があるように見えたので、それが私がしていることです。
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 を実行し、何が送信され、何が受信されているかを確認してください。
HTH