JSON を使用した WCF REST スタイルのサービスがあり、IE では問題なく動作しますが、FireFox と Chrome は癇癪を起こしています。クロスドメインコールについて。
ここのアドバイスに従いました: JQueryからWCF RESTメソッドへのJSONデータの送信に関する問題
しかし、私が今見ているのは、ブラウザが 2 つのリクエスト (最初は OPTIONS、次に POST) を送信しているにもかかわらず、POST リクエストは何も返さないということです。繰り返しますが、IE はこれで完全に動作します。何が欠けていますか?
ジャバスクリプト:
<script type="text/javascript">
function Search() {
var json = JSON.stringify($('#testForm').serializeObject());
$.ajax(
{
type: "POST",
url: "http://localhost:8000/MyService.svc/Search",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response, status, xhr) {
GetResults(response.SearchResult.QueryId);
},
error: function (xhr, status, error) {
alert("Error\n-----\n" + xhr.status + '\n' + xhr.responseText);
},
//complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); }
});
return false;
}
function GetResults(queryId) {
debugger;
$.ajax(
{
type: "GET",
url: "http://localhost:8000/MyService.svc/GetResults?queryId=" + queryId + "&ignoreXmlFeedSourceIds=",
//data: {},
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (response, status, xhr) {
debugger;
DoSomethingWithResults(response);
},
error: function (xhr, status, error) {
alert(error);
},
complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); }
});
}
function DoSomethingWithResults(results) {
alert("In DoSomethingWithResults()");
// process the results here to show on UI
debugger;
};
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
</script>
サービス契約:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Search")]
SearchResponse Search(RequestInfo requestInfo);
[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "/Search")]
void SearchAllowCors();
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetResults?queryId={queryId}&ignoreXmlFeedSourceIds={ignoreXmlFeedSourceIds}")]
IList<FlightJourneyAvailabilityResponse> GetResults(string queryId, string ignoreXmlFeedSourceIds);
}