我正在使用 web api2 在 Asp.Net MVC5 中制作 web api。我发了一个这样的帖子:
public class SmsClientsController : ApiController
{
public void Post(Client client)
{
//Add Client to database
}
}
模型客户端是这样的:
public class Client
{
public int Id { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public int Role { get; set; }
}
我在单击按钮时调用了这个 post 方法,只是从客户端的 javascript 中调用,如下所示:
function SendSms() {
var studentData = {
"Username": "Anjin",
"Password": "Pradhan",
"Role": "1"
};
$.ajax({
type: "POST",
url: "http://192.168.0.102/ProductsApp/api/SmsClients",
data: JSON.stringify(studentData),
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data, status, jqXHR) {
console.log(data);
console.log(status);
console.log(jqXHR);
alert("success..." + data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
但在响应时,它只会提醒一个空白的警报框。当我通过fire bug检查控制台时出现错误:
"NetworkError: 405 Method Not Allowed - http://192.168.0.102/ProductsApp/api/SmsClients"
为什么会这样??这个Post
方法无效吗。请帮我纠正这个...