NetworkError: 405 Method Not Allowed in WCF

2020-07-25 23:09发布

I am trying to call WCF REST service method using Jquery ajax call and getting an error like

"NetworkError: 405 Method Not Allowed - http://localhost:55911/Service1.svc/Testing"

Here is my code

  $(document).ready(function () {
            $("#Button2").click(function () {
                 var Input = {
                     UserId: "11111"
                             };
                $.ajax({
                    type: "POST",
                    url: " http://localhost:55911/Service1.svc/Testing",
                    data: JSON.stringify(Input),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("Success");
                    },
                    error: function (xhr, status, error) {
                        alert("failure");
                        alert(stattus.toString);
                    }
                });

            });
        });

and

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Testing", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Testing(int UserId);

and

  public string Testing(int UserId)
        {
            sqlConn = new SqlConnection(connectionString);
            sqlConn.Open();
            sqlCmd = new SqlCommand("TestInsertion", sqlConn);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@id",UserId);
            sqlCmd.ExecuteNonQuery();
            sqlConn.Close();
            return "true";
        }

What am i doing wrong here??Any suggestion??

EDIT:After commenting //contentType: "application/json" it is posting and throwing

"NetworkError: 400 Bad Request - http://localhost:55911/Service1.svc/Testing"

标签: c# wcf rest jquery
6条回答
够拽才男人
2楼-- · 2020-07-25 23:52
  • Please check that the HTTP Request method is POST at client (html) and on server (WCF service)
  • Because the NetworkError 405 status suggests that the calling code (html) is using wrong HTTP Method, WCF service is working ok.

Please refer: 405 Method Not Allowed Error in WCF

Update

What I believe is keep the rest of the things (also content type contentType: "application/json") as it is just change parameter type to string from int in WCF service

    public string Testing(string UserId)
    {
        sqlConn = new SqlConnection(connectionString);
        sqlConn.Open();
        sqlCmd = new SqlCommand("TestInsertion", sqlConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.Add("@id",UserId);
        sqlCmd.ExecuteNonQuery();
        sqlConn.Close();
        return "true";
    }
查看更多
SAY GOODBYE
3楼-- · 2020-07-25 23:52

Are you running both WCF Service Project and Consuming Project (AJAX Call) at the same time (in same instance of Visual Studio)? If so, try two different VS instances and run WCF Project first, then the consuming project. Also, try using dataType as jsonp instead of json.

查看更多
The star\"
4楼-- · 2020-07-25 23:55

Check whether the HTTP Request method is POST or OPTIONS. This link will be useful.

查看更多
男人必须洒脱
5楼-- · 2020-07-25 23:55

I think you need to use JSONP for a cross-domain call to get round the browser restrictions

this answer is very helpful: WCF error : 405 Method Not Allowed

查看更多
手持菜刀,她持情操
6楼-- · 2020-07-25 23:58

The UriTemplate you defined is wrong, try using it like this:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Testing?U={UserId}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Testing(string UserId);

and then in the implementation convert that UserId to int as you wanted.

查看更多
何必那么认真
7楼-- · 2020-07-26 00:10

Usually this errors happens when an exception is thrown at your method so i suggest to debug

it either by attaching it to process to by writing this line of code at the beginning of the

function

Debugger.launch();
查看更多
登录 后发表回答