2013-05-09 106 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