dongzhong7299 2015-11-05 06:09
浏览 106
已采纳

如何使用jQuery Ajax将JavaScript数组项加载到MySQL数据库

I Have a simple array which looks like

var arr = ["foo", "2015/11/04", "Jill", "Smith", "60"]
var serializedArr = JSON.stringify( arr );

now can you please let me know how I can load the serializedArr into the data in following ajax request?

var upload = $.ajax({
    type: "POST",
    url: "assets/.../loader.php",
    data: data,
    cache: false,
    beforeSend: function() {
    }
});

and also how to read the data on server side PHP file? Thanks

  • 写回答

1条回答 默认 最新

  • dongpou1935 2015-11-05 06:14
    关注
    var upload = $.ajax({
        type: "POST",
        url: "assets/.../loader.php",
        data: {array:serializedArr},// pass here{key:value,key:value,...}
        cache: false,
        beforeSend: function() {
        }
    });
    

    In php

    $array = json_decode($_POST['array']);
    print_r($array);// this will get back the array which was encoded.
    

    You have to decode the array because, array is encoded(JSON.stringify(arr))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?