简体   繁体   English

对C#Controller的HTTP POST请求

[英]HTTP POST request to C# Controller

I'm trying to make HTTP POST request to my C# controller, but I need to send in data an array, so I tried with JSON.stringify but when I start debugging, the input parameter in my controller is NULL? 我正在尝试向我的C#控制器发出HTTP POST请求,但是我需要向数据发送一个数组,所以我尝试使用JSON.stringify,但是当我开始调试时,我的控制器中的输入参数是NULL? I'm receiving a list from external API for weather forecast, so I need to create for each item in list new variable, that has some fields like : max and min temperature, description, humidity, pressure etc, and then of course fill these fields with data and add that variable to my array. 我正在从外部API接收天气预报列表,所以我需要为列表中的每个项目创建新变量,它有一些字段,如:最大和最小温度,描述,湿度,压力等,然后当然填写这些带有数据的字段并将该变量添加到我的数组中。 Then I need to pass this array to my controller so I could store it in my database... What type should I put in my controller so it would not be NULL? 然后我需要将这个数组传递给我的控制器,这样我就可以将它存储在我的数据库中...我应该在控制器中放入什么类型,这样它就不会是NULL? I'm totally new here so please help, any help is really more then welcome! 我是全新的,所以请帮助,任何帮助真的更受欢迎!

Below is code I have just to try : 以下是我要尝试的代码:

 var myData = { id:100, description:"some text"};
 var myDataArray= new Array();   
 myDataArray.push(myData);   
 $.ajax({
    dataType: "json",
    type: "POST",
    url: "/Weather1/Weather_post",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(myDataArray),
    success: function (data) {
        console.log(("OK"));
    },
    error: function (error)
    { console.log("NOT OK"); }
})

Controller: 控制器:

[HttpPost]
public JsonResult Weather_post(String MyModuleList)

Model binding has no idea what "MyModuleList" is. 模型绑定不知道“MyModuleList”是什么。 You can use a strongly typed model here and MVC will bind the JSON to it. 您可以在此处使用强类型模型,MVC会将JSON绑定到它。

Consider JSON: 考虑JSON:

var data = {
    moduleList: [
        { id:100, description:"some text"}
    ];
};

and models: 和型号:

public class ModuleListModel
{
    public List<ModuleModel> ModuleList { get; set; }
}
public class ModuleModel
{
    public int Id { get; set; }
    public string Description { get; set; }
}

and action: 和行动:

[HttpPost]
public JsonResult Weather_post(ModuleListModel model)
{ 
    ... 
}

Combine that with @Timothy Shields' answer: 结合@Timothy Shields的回答:

You're missing processData: false in your ajax call. 您在ajax调用中缺少processData:false。 Without that, ajax is going to try to pack the data into the URL query string. 没有它,ajax将尝试将数据打包到URL查询字符串中。 (See here: http://api.jquery.com/jQuery.ajax/ ) (见这里: http//api.jquery.com/jQuery.ajax/

and you should be good. 你应该好。

You're missing processData: false in your ajax call. 您在ajax调用中缺少processData: false Without that, ajax is going to try to pack the data into the URL query string. 没有它, ajax将尝试将data打包到URL查询字符串中。 (See here: http://api.jquery.com/jQuery.ajax/ ) (见这里: http//api.jquery.com/jQuery.ajax/

If data is { 'animal': 'elephant', 'count': 5 } and processData is true (the default), ajax will do the POST to the URL /Weather1/Weather_post?animal=elephant&count=5 with an empty HTTP request body. 如果data{ 'animal': 'elephant', 'count': 5 }processDatatrue (默认值),则ajax将使用空HTTP请求对URL /Weather1/Weather_post?animal=elephant&count=5进行POST身体。 This is why you want processData: false . 这就是你想要processData: false

Try as follows: 尝试如下:

 var myData = { id:100, description:"some text"};
 var myDataArray= new Array();   
 myDataArray.push(myData); 
 var param = JSON.stringify(myDataArray);  
 $.ajax({
     dataType: "json",
     type: "POST",
     url: "/Weather1/Weather_post",
     contentType: "application/json; charset=utf-8",
     data: {'MyModuleList': param },
     success: function (data) {
          console.log(("OK"));
     },
     error: function (error)
     { console.log("NOT OK"); }
 })

You may need to pass the parameter name with the data. 您可能需要将参数名称与数据一起传递。 Something like: 就像是:

 data: {'MyModuleList': JSON.stringify(myDataArray)},

See if this works for you. 看看这是否适合你。

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

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