简体   繁体   English

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

[英]call c# method with parameters from js ajax

when calling ac# method from js ajax I am getting 404 error on console当从 js ajax 调用 ac# 方法时,我在控制台上收到 404 错误

I tried following ways to send data to c# method我尝试了以下方法将数据发送到 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# method is something like this C# 方法是这样的

[HttpPost]
public HttpResponseMessage AddCertificateFromStore(string CertificateName)
{
    // Make the web request
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

    return response;
}

Would like to send the parameters to C# method correctly so that it is being called from js ajax想将参数正确发送到 C# 方法,以便从 js ajax 调用它

You should add a FromBody decorador in your C# Action您应该在 C# 操作中添加 FromBody 装饰器

[HttpPost] public HttpResponseMessage AddCertificateFromStore([FromBody] string CertificateName) { [HttpPost] public HttpResponseMessage AddCertificateFromStore([FromBody] string CertificateName) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM