2013-05-09 105 views
1

這是我的WCF Web服務WCF REST服務郵政法JSON格式

[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json2")] 
StringMessageJson AddEmployee(EmployeeJson emp); 

代碼,這是我在服務

public StringMessageJson AddEmployee(EmployeeJson emp) 
{ 

} 

在這裏,我的CS文件代碼我正在通過jquery ajax調用webservice方法:

$('document').ready(function() 
{ 
    var emp = 
    { 
     ID: '', 
     EmpName: 'Sk', 
     EmpID: 'A004', 
     Username: '[email protected]', 
     Password: 'Aaa', 
     Address: 'Sec-001', 
     PhoneNumber: '09', 
     MobileNumber: '535345345', 
     EmpTypeID: '2', 
     EmpTypeValue: '' 
    }; 

    var datatosend='{"emp":' + JSON.stringify(emp) + '}'; 
    $.ajax(
    { 
     type: "POST", 
     url: "http://localhost:6943/EmsRestApi.svc/json2", 
     data: datatosend, 
     dataType: "jsonp", 
     contentType: "application/json; charset=utf-8", 
     success: function (resp) 
     { 
      Console.log(resp); 
      alert("Message\n'" + resp.Message + "'"); 
     }, 
     error: function (e) 
     { 
      //console.log(e); 
      alert(e); 
     } 
    }); 
     $.support.cors = true; 
}); 

並運行此之後,我的Chrome控制檯上收到錯誤消息: -

無法加載資源:服務器與405狀態響應(不允許的方法)

請幫我擺脫解決這個問題。

+0

查看相關答案(http://stackoverflow.com/a/14122895/1810429)FWIW。 – J0e3gan 2013-05-09 07:11:30

+1

當我通過谷歌瀏覽器中的開發人員工具進行檢查時,它顯示** GET **請求正在發送而不是Post。 – Sonu 2013-05-09 07:27:13

回答

0

通過審查上面的代碼片段,我可以看到你沒有指定對WCFService WebInvoke屬性的請求格式。

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json2", RequestFormat=WebMessageFormat.Json)] 

這種錯誤發生在服務器無法找到要執行的WCF OperationContract時。

+0

我已經使用它,但我得到相同的迴應。所以,我沒有寫在這裏。 – Sonu 2013-05-09 07:51:32

+0

如果你可以在web.config中爲我提供配置相關的東西,它將會是greate。 – 2013-05-09 12:40:49