我已经在ASP.NET中创建了Web API,首先我创建了Controllers,然后通过脚手架创建了一个空的UI并更改了RoutingConfig文件,然后创建了 WebApi 2 Controller-Empty strong>,写了两种方法, 1)获取方法2)发布方法....从我的Jquery ajax调用时,获取方法可以正常工作,但不适用于发布方法。
我得到的方法未找到405状态代码。I have attached the error message image
答案 0 :(得分:0)
// Ajax call
function MyMethod() {
var guid = document.getElementById("guid").value;
var fname = document.getElementById("firstname").value;
var lname = document.getElementById("lastname").value;
var jsonString = '{"Id": "'+guid+'","firstname":"'+fname+'" ,"lastname": "'+lname+'"}';
$.ajax({
url: '/api/WebApi/Post',
type: 'Post',
contentType: "application/json; charset='UTF-8'",
data: JSON.stringify(jsonString),
datatype:"text",
success: function () {
console.log("Success");
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
// WebApi Controller
[HttpPost]
public string Post([FromBody]string data) {
Console.Write(data);
return "Hello";
}
[Output of the program][1]
[1]: https://i.stack.imgur.com/dJtqZ.png