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"