我創建了一個呼叫我的 MVC 應用程式的測驗 Web 應用程式。我可以POST通過 Postman呼叫罰款,并想嘗試從 Web 瀏覽器呼叫它,但出現 405 錯誤。任何幫助都會很棒。
加載資源失敗:服務器回應狀態為 405(方法不允許)
測驗 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)
{
}
}
網頁配置
<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>
uj5u.com熱心網友回復:
您必須修復 ajax.remove 標頭和內容型別并修復資料型別并添加資料
$.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));
}
});
并從操作中洗掉帖子
public IHttpActionResult MethodTest(string key, string userName, string successUrl)
{
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/339679.html
標籤:C# asp.net-mvc asp.net-ajax
