AJAX Post 测试方法加载资源失败
AJAX Post Test Method Failed to load resource
我创建了一个调用我的 MVC 应用程序的测试 Web 应用程序。我可以使用 Postman 正常调用 POST
并想尝试从 Web 浏览器调用它,但出现 405 错误。任何帮助都会很棒。
Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
测试HTML项目
$.ajax({
type: "POST",
url: "URL",
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
crossDomain: true,
success: function (response) {
console.log('message: ' + "success" + JSON.stringify(json));
},
failure: function (error) {
console.log('message Error' + JSON.stringify(error));
}
});
MVC 应用程序
public class PublicController : ApiController
{
[System.Web.Http.HttpPost]
public IHttpActionResult MethodTest(string key, string action)
{
}
}
web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>
您必须修复 ajax.remove headers 和内容类型并修复数据类型并添加数据
$.ajax({
type: "POST",
url: "http://localhost:60876/WebApi/public/SkipAuthenticationAndLogin"
data:{
key:"key",
userName:"name",
succesUrl:"url"
},
dataType: "json",
success: function (response) {
console.log('message: ' + "success" + JSON.stringify(response));
},
failure: function (error) {
console.log('message Error' + JSON.stringify(error));
}
});
并从操作中删除 post
public IHttpActionResult MethodTest(string key, string userName, string successUrl)
{
}
我创建了一个调用我的 MVC 应用程序的测试 Web 应用程序。我可以使用 Postman 正常调用 POST
并想尝试从 Web 浏览器调用它,但出现 405 错误。任何帮助都会很棒。
Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
测试HTML项目
$.ajax({
type: "POST",
url: "URL",
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
crossDomain: true,
success: function (response) {
console.log('message: ' + "success" + JSON.stringify(json));
},
failure: function (error) {
console.log('message Error' + JSON.stringify(error));
}
});
MVC 应用程序
public class PublicController : ApiController
{
[System.Web.Http.HttpPost]
public IHttpActionResult MethodTest(string key, string action)
{
}
}
web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>
您必须修复 ajax.remove headers 和内容类型并修复数据类型并添加数据
$.ajax({
type: "POST",
url: "http://localhost:60876/WebApi/public/SkipAuthenticationAndLogin"
data:{
key:"key",
userName:"name",
succesUrl:"url"
},
dataType: "json",
success: function (response) {
console.log('message: ' + "success" + JSON.stringify(response));
},
failure: function (error) {
console.log('message Error' + JSON.stringify(error));
}
});
并从操作中删除 post
public IHttpActionResult MethodTest(string key, string userName, string successUrl)
{
}