简体   繁体   English

有一个JavaScript对象数组作为ajax调用参数,而没有jquery?

[英]Having an array of javascript objects as an ajax call parameter, without jquery?

I'm trying to send an array of JavaScript objects to my ASP.Net MVC controller without using jQuery . 我正在尝试不使用jQuery将JavaScript对象数组发送到ASP.Net MVC控制器。 I have a simple array of JavaScript objects like this... 我有一个简单的JavaScript对象数组,像这样...

var data = [];

data.push(new person(1, "David"));
data.push(new person(2, "Karine"));
data.push(new person(2, "Annie"));
...etc

function person(_id, _name) {
    this.id = _id;
    this.name = _name;
}

...and I would like to have it as a parameter of an ajax call like this: ...而且我想像这样将其作为ajax调用的参数:

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                    var obj = JSON.parse(xmlhttp.responseText);
                        for (var i = 0; i < obj.length; i++) {                                                
                            if (i == 1) {
                                    alert(obj[i].name);}
                        }
                }
                else {
                    alert('There is an error');
                }
    }
};

xmlhttp.open("GET", "/Home/AjaxTester?id=15&name=dave", true);                             
xmlhttp.send();

The way I found so far to pass parameters to an ajax call, which is ...Home/AjaxTester?id=15&name=dave , works fine for basic types, but I can't find a way to do the same with more "complex" types. 到目前为止,我发现了将参数传递给ajax调用的方式,即...Home/AjaxTester?id=15&name=dave ,对于基本类型来说效果很好,但是我找不到找到更多方法来执行此操作的方法复杂”类型。 I'm open to new ways to parameterize my ajax call if anything better, or even a new way to make an ajax call, as long as it is pure JavaScript, no library, no framework, or whatever. 我愿意采用新方法来参数化ajax调用(如果有更好的方法),甚至可以采用新方法进行ajax调用,只要它是纯JavaScript,无库,无框架等即可。

Here's my MVC controller... 这是我的MVC控制器...

public JsonResult AjaxTester(List<Person> _person)
{
    //Whatever Logic...

    return Json(_someObjects);
}

...and the class of the received parameter ...以及所接收参数的类别

public class Person
{
    public int id { get; set; } = 0;      
        public string name { get; set; } = "";

        public Person(int _id, string _name)
        {
            this.id = _id;
            this.name = _name;
        }
}

您需要在参数之前使用FromUri命令,如下所示:

public JsonResult AjaxTester(FromUri List<Person> _person)

You need to change your http method from GET to POST. 您需要将http方法从GET更改为POST。

xmlhttp.open("POST", "/Home/AjaxTester?id=15&name=dave", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(JSON.stringify(data)); 

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/send

Make sure you add the setRequestHeader. 确保添加setRequestHeader。 If you are sending a json object then change the request header to the following: 如果要发送json对象,则将请求标头更改为以下内容:

xhr.setRequestHeader("Content-type", "application/json");

I would also recommend removing the query parameter and updating the controller to accept this type of object. 我还建议删除查询参数并更新控制器以接受这种类型的对象。 You may also need to add the [FromBody] in next to that parameter. 您可能还需要在该参数旁边添加[FromBody]。

And your controller should be updated similar to 并且您的控制器应该像

public JsonResult AjaxTester([FromBody] List<Person> _person)

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

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