我正在尝试使用 Jquery ajax 调用来调用 WCF REST 服务方法并收到类似的错误
"NetworkError: 405 Method Not Allowed - http://localhost:55911/Service1.svc/Testing"
这是我的代码
$(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);
}
});
});
});
和
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Testing", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Testing(int UserId);
和
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";
}
我在这里做错了什么?有什么建议吗?
编辑:评论后 //contentType: "application/json"
发布和投掷
"NetworkError: 400 Bad Request - http://localhost:55911/Service1.svc/Testing"