我正在使用跨域 jQuery ajax 调用我的 WCF Web 服务。我正在使用 CORS 方法,但错误 block 没有为我触发。当我尝试 jsonp 方法时,它正在触发。请查看代码。
CORS:
function faultCLick() {
$.support.cors = true;
$.ajax({
url: "http://mydomain:84/AuthService.svc/ErrorHandling",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function res(msg) {
jsonpTest = msg;
alert("inside success ");
},
error: function (message) { // not firing
debugger;
var jsonFault = JSON.parse(message.responseText);
alert(jsonFault.Message);
}
});
}
服务:-
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "ErrorHandling")]
string ErrorHandling();
谢谢。
最佳答案
我不知道为什么它没有早点发射。现在工作正常。这是我的代码。
$.support.cors = true;
$.ajax({
type: "GET",
dataType: "json",
contentType: "application/json;charset=utf-8",
url: "http://mydomain:89/MyAppAppService.svc/GetFolders",
success: function (msg) {
alert("Success");
},
error: function (jqXHR, status, message) {
alert(jqXHR.responseText);
alert(status + " " + message);
}
});
谢谢。
关于javascript - 跨域错误处理和WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18008660/