简体   繁体   中英

Passing javascript array through ajax with php

I have an array that is created in my ajax php file that looks like this

[[1,2,3],[1,2,3]]

So I echo it with a json_encode

echo json_encode($array);

This is my ajax code

$.ajax(
                {
                    url: "ajaxfile.php" + "?something=" + something + "&something2=" + something2 + "&something3=" + something3,
                    type: "POST",
                    data: JSON,
                    success: function (data) {
                        object = data;
                        functionIwantthearraytobepassedto();
                    }
                }
            )

In the console all I get is


: Array to string conversion in on line :第行的数组到字符串的转换
"[Array]"

Where am I going wrong? And how can I fix it?

Please try to add a header:

header('Content-Type:text/json');
echo json_encode($array);

Try adding to your ajax
dataType: 'json'

and

cotentType: 'application/json; charset=utf-8'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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