简体   繁体   English

在javascript中动态添加值

[英]dynamically adding the value in javascript

I am very new to javascript. 我对javascript很陌生。 I have a requirement to make a server call, get the json response and parse the response and populate the formatted response to a variable inside a object literal. 我需要进行服务器调用,获取json响应并解析该响应,然后将格式化后的响应填充到对象文字内部的变量中。

Its looks something like this below: 如下所示:

$('#dataTables-example').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bAutoWidth": true,
        "aaData":
        [
            [
                "",
                "1",
                "name-key",
                "description"
            ],
            [
                "",
                "2",
                "name",
                "description"
            ],
            [
                "",
                "23",
                "name",
                "description"
            ],
            [
                "",
                "24",
                "abs",
                "Common"
            ],
            [
                "",
                "5",
                "name1",
                "description"
            ],
            [
                "",
                "6",
                "name2",
                "description"
            ]
        ]
    });

In the above, I need to populate the aaData variable with json data from a server call. 在上面,我需要使用来自服务器调用的json数据填充aaData变量。 Please let me know how to get this done in javascript. 请让我知道如何使用javascript完成此操作。

What you want is to call the server via AJAX. 您要通过AJAX调用服务器。 JQuery handles this for you quite easily. jQuery很容易为您处理此问题。

Check out this link: https://api.jquery.com/jQuery.ajax/ 查看此链接: https : //api.jquery.com/jQuery.ajax/

A simple example of using AJAX: 一个使用AJAX的简单示例:

$.ajax({
    url: 'your url here'
    success: function(data, textStatus, jqXHR) {
        console.log(data)
    }
});

You will want to make this call and set the data from the success function to a variable, format it correctly (possibly using for loops), and then use that variable as your 'aaData' value. 您将需要进行此调用并将成功函数中的数据设置为变量,正确设置其格式(可能使用for循环),然后将该变量用作“ aaData”值。

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

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