這是任何 Jquery ajax POST 請求;
$.ajax({
type: "POST",
url: "/Customers/GetAll",
contentType: "application/json",
dataType: "json",
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
這是上面ajax請求的我的控制器操作方法;
public IActionResult GetAll()
{
//Method body...
}
如何dataType在基本控制器或操作方法中獲取任何 ajax 請求?
uj5u.com熱心網友回復:
嘗試進入accept標題:

行動:
public IActionResult GetAll()
{
string dataType = HttpContext.Request.Headers["accept"].ToString();
//Method body...
}
uj5u.com熱心網友回復:
可以在 HttpContext 物件上檢索標頭。
if(HttpContext.Request.Headers.TryGetValue("accept", out var acceptHeaders)) {
var firstAcceptHeader = acceptHeaders.FirstOrDefault();
}
您還可以使用 indexing 訪問標頭值Headers["accept"],但請注意,如果沒有接受標頭,無論出于何種原因,這都會引發 NullReferenceException。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347656.html
標籤:阿贾克斯 asp.net核心 asp.net-core-mvc asp.net-core-3.1
