如何在$ .ajax中传递多个参数?

时间:2014-02-28 16:45:32

标签: jquery ajax

我想在$ .ajax调用中传递2个参数。

这是我的代码:

function getStatistic6() {

var response;
var allstat6 = [];
var dstart = "01/01/2014";
var dend = "31/03/2014";
$.ajax({
    type: 'GET',
    url: 'http://localhost:52251/Service1.asmx/Statistic_6_Entete',
    data: "start='" + dstart + "'" + " end='" + dend +"'",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
        response = msg.d;
        for (var i = 0; i < response.Items.length; i++) {
            var j = 0;
            allstat6[i] = [response.Items[i].Date, response.Items[i].Piece, response.Items[i].Tiers, response.Items[i].AmoutHT, response.Items[i].AmountTTC, response.Items[i].Quantite];
        }
        fillDataTable6(allstat6);
        $('table').visualize({ type: 'line' });

    },
    error: function (e) {
        alert("error loading statistic 6");
    }
});
}

我有一个错误:“无效的JSON原语:end = '31 / 03/2014'。

有一个参数,我做得很好。

如何传递2个参数?

3 个答案:

答案 0 :(得分:2)

您可以将其作为对象传递,如下所示

function getStatistic6() {

    var response;
    var allstat6 = [];
    var dstart = "01/01/2014";
    var dend = "31/03/2014";
    $.ajax({
        type: 'GET',
        url: 'http://localhost:52251/Service1.asmx/Statistic_6_Entete',
        //pass it as an object, jQuery will serialize it for you
        data: {
            start: dstart,
            end: dend
        },
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            response = msg.d;
            for (var i = 0; i < response.Items.length; i++) {
                var j = 0;
                allstat6[i] = [response.Items[i].Date, response.Items[i].Piece, response.Items[i].Tiers, response.Items[i].AmoutHT, response.Items[i].AmountTTC, response.Items[i].Quantite];
            }
            fillDataTable6(allstat6);
            $('table').visualize({
                type: 'line'
            });

        },
        error: function (e) {
            alert("error loading statistic 6");
        }
    });
}

答案 1 :(得分:0)

 data: {start: dstart, end: dend }

修改 试试:

 data: {"start": dstart, "end": dend }

答案 2 :(得分:0)

小修正,删除's并添加“&amp;”每个值之间。

data: "start=" + dstart + "&" + " end=" + dend