1

这是我的 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(不允许方法)

请帮助我摆脱这个问题。

4

1 回答 1

0

通过查看上面的代码片段,我可以看到您没有为 WCFService WebInvoke 属性指定 requestformat。

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

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

于 2013-05-09T07:41:57.690 回答