[英]wcf rest Service Post method with json format
這是我的WCF Web服務代碼
[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json2")]
StringMessageJson AddEmployee(EmployeeJson emp);
這是我在服務的CS文件中的代碼
public StringMessageJson AddEmployee(EmployeeJson emp)
{
}
在這里,我通過jquery ajax調用webservice方法:
$('document').ready(function ()
{
var emp =
{
ID: '',
EmpName: 'Sk',
EmpID: 'A004',
Username: 'A@a.a',
Password: 'Aaa',
Address: 'Sec-001',
PhoneNumber: '09012312',
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(不允許使用方法)
請幫助我擺脫這個問題。
通過查看上面的代碼片段,我可以看到您尚未為WCFService WebInvoke屬性指定requestformat。
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json2", RequestFormat=WebMessageFormat.Json)]
當服務器找不到要執行的WCF OperationContract時,會發生這種錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.