我嘗試使用 ajax 呼叫在 .net 核心中呼叫此 onget 方法,但此方法在沒有引數的情況下命中
public void OnGet(string id,string refundReason ,int amount)
{
}
我使用了這 2 個 ajax 呼叫但沒有傳遞引數值
$.ajax({
url: "https://localhost:7197/Transactions/Refund?id=" '20a63762-a6ab-4edb-852b-fd247e9dc247' "&refundReason=" refundReason "&amount=" amount,
type: "GET",
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
debugger;
},
});
這一個也嘗試不將值傳遞給 onget 方法。讓我知道我要做哪些改變。謝謝
$.ajax({
url: "https://localhost:7197/Transactions/,
type: "GET",
data: JSON.stringify({ id: '20a63762-a6ab-4edb-852b-fd247e9dc247',
refundReason:'tt',
amount:"1"
}),
uj5u.com熱心網友回復:
首先,您應該指明您希望如何從客戶端獲取引數。嘗試在引數上使用[FromQuery], [FromBody],[FromForm]屬性。您還需要指定您的請求方法:[HttpGet],[HttpPost]等。
其次,在您發布的代碼中,您從正文和查詢字串發送資料。您只需要使用其中之一。
這應該有效:
[HttpGet]
public void OnGet([FromQuery]string id,[FromQuery]string refundReason, [FromQuery]int amount)
在 AJAX 代碼中,洗掉該data部分,并確保您為 AJAX 代碼提供了正確的 URL 引數。
uj5u.com熱心網友回復:
您應該能夠呼叫它$.get并傳遞查詢字串中的值。GET 呼叫不接受正文資料。
$.get(`https://localhost:7197/Transactions/Refund?id=20a63762-a6ab-4edb-852b-fd247e9dc247&refundReason=${refundReason}&amount=${amount}`, response => {
...
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/497158.html
