2013-08-02 68 views
1

我正在使用跨域jQuery ajax调用我的WCF web服务。我正在使用CORS的方法,但是错误块并不适合我。当我尝试jsonp方法时,它正在发射。请完成代码。跨域错误处理和WCF

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(); 

感谢。

回答

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

谢谢。