1

我正在对我的 WCF web 服务使用跨域 jQuery ajax 调用。我正在使用 CORS 方法,但错误块没有为我触发。当我尝试 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();

谢谢。

4

1 回答 1

0

我不知道为什么它没有早点开火。现在它工作正常。这是我的代码。

    $.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);
        }
    });

谢谢。

于 2013-08-28T08:25:03.277 回答