1

This is my code for wcf web services

[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

public StringMessageJson AddEmployee(EmployeeJson emp)
{

}

And here I am calling the webservice method through jquery ajax:

$('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:-

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

Please Help me to get rid off this issue.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sonu
  • 458
  • 5
  • 13
  • Check out a [related answer](http://stackoverflow.com/a/14122895/1810429) FWIW. – J0e3gan May 09 '13 at 07:11
  • 1
    When i check through the developer tools in google crome browser, it shows the **GET** Request is being send instead of Post. – Sonu May 09 '13 at 07:27

1 Answers1

0

By Reviewing above snippet I can see that you have not specified requestformat to the WCFService WebInvoke Attribute.

[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.

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62