[英]call c# method with parameters from js ajax
當從 js ajax 調用 ac# 方法時,我在控制台上收到 404 錯誤
我嘗試了以下方法將數據發送到 c# 方法
data: '{ name: "test" }'
data: JSON.stringify({ 'name': 1234 }),
$.ajax({
url: "/api/system/AddCertificateFromStore",
type: 'POST',
data: '{ name: "divya" }',
complete: function (xhr, status) {
},
error: function (xhr, status, err) {
}
},
cache: false,
contentType: "application/json; charset=utf-8",
processData: false,
dataType: 'json'
C# 方法是這樣的
[HttpPost]
public HttpResponseMessage AddCertificateFromStore(string CertificateName)
{
// Make the web request
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
return response;
}
想將參數正確發送到 C# 方法,以便從 js ajax 調用它
您應該在 C# 操作中添加 FromBody 裝飾器
[HttpPost] public HttpResponseMessage AddCertificateFromStore([FromBody] string CertificateName) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.