2

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"
Rooney
  • 827
  • 7
  • 15
  • 21
  • I am very confused now. Are you trying to use WCF or are you trying to use Web API. Because you have syntax from both and they aren't compatible. – Aron Feb 12 '13 at 08:30
  • @Aron am using WCF only..Which one is Web API syntax?? – bala3569 Feb 12 '13 at 09:27
  • Web invoke is a web api attribute. I suggest if you want to use that syntax to switch to using asp.net MVC and create a web api controller. – Aron Feb 12 '13 at 12:23
  • PS web invoke is likely to do nothing to your wcf contract. – Aron Feb 12 '13 at 12:23
  • @Rooney check my updated [answer](http://stackoverflow.com/a/14831359/468718). – Harsh Baid Feb 12 '13 at 13:57

6 Answers6

1
  • 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";
    }
Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
1

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
Soheil
  • 11
  • 1
0

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();
0

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.

Firnas
  • 1,665
  • 4
  • 21
  • 31
  • No need to comment `contentType`. As Harsh also mentioned, you cannot put `int` type parameter. So, change it to `string` – Firnas Feb 13 '13 at 04:55
0

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

kk1076
  • 1,740
  • 13
  • 45
  • 76
0

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

Community
  • 1
  • 1
Rasoul Zabihi
  • 2,575
  • 2
  • 18
  • 10