Ajax post,将数组数组转换为字符串数组

时间:2016-08-02 09:03:18

标签: javascript arrays ajax node.js

我正试图从.ejs页面发帖到nodejs api。

这是我的电话

var dataSend={
    from:from,
    to:to,
    datesStart: startDates,
    datesEnd: endDates,
    price: totalPriceOften,
    places:JSON.stringify(placesArray),
    package: pSizeOften,
    transport: $('.activeFilter:eq(1) a').html().toLowerCase(),
    pickupRange: pFlexOften,
    dropRange: dFlexOften,
};

$.ajax({
    url: location.origin + '/publishTravelOften',
    headers: {
        apiKey: APIKEYWEB
    },
    type: "POST",
    data: dataSend,
    dataType: "json",
    async: true,
    success: function (data) {
        //do stuff
    },
    error: function (err) {
        //do other stuff
    }
});

我发送的数据是带有此信息的对象

enter image description here

在nodejs api中,我有这个:

  router.post('/publishTravelOften', function (req, res) {
    if(req.user == null || req.user == undefined){
        res.status(403).send(Error.ERROR_NO_USER);
    }
    else {
        var time = Date.now();
        var userLanguage = Geo.getCountry(req);
        console.log("LOAD TIME /publishTravelOften: " + (Date.now() - time));


        Config.setCountry(Geo.getCountry(req));

        publishTravelOften(req, function (error, data) {
            if (!error) {
                data.lang = Lang.getLang(userLanguage);
                data.socketUrl = Config.getSocketUrl();
                data.apiUrl = Config.getApiUrlWeb();

                res.send(data);
            }
            else {
                res.status(500).send(error);
            }
        });
    }
});

function publishTravelOften(req,callback){

    console.log("########");
    console.log(req.body);
    console.log("########");

    var url = Config.getApiUrlWeb() + "travel/often/create?user="+req.user._id+"&currency="+Geo.getCountry(req);

    var options = {
        url: url,
        form:req.body,
        headers: {
            'apikey': Config.getAPIKeyWeb()
        },
    };

    request.post(options, function (error, response, body) {
        var parsedResponse = JSON.parse(body);
        if (parsedResponse.success == false)
            callback(parsedResponse, parsedResponse);
        else {
            var data = {
                newTravel: parsedResponse.data
            };
            callback(error, data);
        }
    });
}

我的问题是,当我打印出nodejs部分的数据时,我有了这个

    { from: 'ChIJO_PkYRozGQ0R0DaQ5L3rAAQ',
  to: 'ChIJ9xUaAY73vEcRUt8vzFOSk1w',
  'datesStart[]': [ '1471683240000', '1471596840000' ],
  'datesEnd[]': [ '1471683240000', '1471596840000' ],
  price: '134.93',
  places: '[]',
  package: 'medium',
  transport: 'airplane',
  pickupRange: '15',
  dropRange: '5' }

数字数组转换为字符串数组,字段名称也从datesStart和datesEnd更改为" datesStart []'和' datesEnd []' 这是正常的吗?以及如何防止数组从数字更改为字符串数组?

0 个答案:

没有答案