[英]wcf rest Service Post method with json format
This is my code for wcf web services 这是我的WCF Web服务代码
[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json2")]
StringMessageJson AddEmployee(EmployeeJson emp);
And this is my code in cs file of services 这是我在服务的CS文件中的代码
public StringMessageJson AddEmployee(EmployeeJson emp)
{
}
And here I am calling the webservice method through jquery ajax: 在这里,我通过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;
});
And after running this I am getting error message on Chrome console:- 运行此命令后,我在Chrome控制台上收到错误消息:-
Failed to load resource: the server responded with a status of 405 (Method Not Allowed) 加载资源失败:服务器响应状态为405(不允许使用方法)
Please Help me to get rid off this issue. 请帮助我摆脱这个问题。
By Reviewing above snippet I can see that you have not specified requestformat to the WCFService WebInvoke Attribute. 通过查看上面的代码片段,我可以看到您尚未为WCFService WebInvoke属性指定requestformat。
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json2", RequestFormat=WebMessageFormat.Json)]
This kind of error occurs when Server can not find WCF OperationContract to be executed. 当服务器找不到要执行的WCF OperationContract时,会发生这种错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.