通过jquery ajax请求传递数组? [英] Pass array through jquery ajax request?

查看:382
本文介绍了通过jquery ajax请求传递数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用ajax请求将数组传递到php页面.当前响应为ction Array() { [native code] },而不是实际的数组内容(im肯定不为空).这是我的代码:

Trying to pass an array to a php page using an ajax request. The current response is ction Array() { [native code] } as opposed to the actual array contents (which im positive is not empty). Here my code:

    function GetPaginationPage(array1) {
    var jsonString = JSON.stringify(array1);

    $.ajax({
        type: "POST",
        url: "includes/get_pagination_page.php",
        data: {data : jsonString},
        success: function(data){ 
        $('.contestants_list').append(data);
        }
        });
};

已用json更新,现在已传递未定义"

UPDated with json, its now passing 'undefined'

推荐答案

没有理由在此处使用JSON.stringify.只需将数组正常发送到PHP.

There's no reason to use JSON.stringify here. Just send the array normally to PHP.

function GetPaginationPage(array1) {
    $.ajax({
        type: "POST",
        url: "includes/get_pagination_page.php",
        data: {
            data: array1
        },
        success: function (data) {
            $('.contestants_list').append(data);
        }
    });
}

现在在PHP中,$_POST['data']将是一个数组.

Now in PHP, $_POST['data'] will be an array.

更新:您说过要像这样呼叫GetPaginationPage:

UPDATE: You said you're calling GetPaginationPage like this:

GetPaginationPage(<?php echo $contestants_array; ?>);

您需要将其更改为:

GetPaginationPage(<?php echo json_encode($contestants_array); ?>);

在PHP中echo数组时,它将转换为字符串"Array",JavaScript将该字符串解释为Array对象.

When you echo an array in PHP, it gets converted to the string "Array", which is intrepreted by JavaScript as the Array object.

这篇关于通过jquery ajax请求传递数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆