【问题标题】:wcf rest Service Post method with json formatjson格式的wcf rest Service Post方法
【发布时间】:2013-05-09 07:05:52
【问题描述】:

这是我的 wcf 网络服务代码

[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(方法不允许)

请帮我解决这个问题。

【问题讨论】:

  • 查看related answer FWIW。
  • 当我在 google crome 浏览器中检查开发者工具时,它显示 GET 请求正在发送而不是 Post。

标签: .net wcf rest


【解决方案1】:

通过查看上面的 sn-p,我可以看到您没有为 WCFService WebInvoke 属性指定 requestformat。

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

当Server找不到要执行的WCF OperationContract时会出现这种错误。

【讨论】:

  • 我已经使用过它,但我得到了相同的响应。所以,我没有写在这里。
  • 如果能在web.config中提供配置相关的东西就更好了。
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 2018-05-19
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
相关资源
最近更新 更多