使用来自js ajax的参数调用c#方法

时间:2019-08-27 17:32:23

标签: javascript c# parameters

从js ajax调用c#方法时,我在控制台上收到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调用它

1 个答案:

答案 0 :(得分:1)

您应该在C#Action中添加FromBody装饰器

[HttpPost] 公共HttpResponseMessage AddCertificateFromStore([FromBody]字符串CertificateName)         {