AJAX Post Test Method加载资源失败



我创建了一个调用MVC应用程序的测试web应用程序。我可以调用POST与邮差很好,并想尝试从web浏览器调用它,但我得到一个405错误。如果有任何帮助就太好了。

加载资源失败:服务器响应状态为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。删除header和contenttype,修复dataType并添加data

$.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));
}  
});  

并从action

中移除post

public IHttpActionResult MethodTest(string key, string userName, string successUrl)
{
}

最新更新