我有这个硬编码的JS二维数组:
var d = [
["05_001","05_002","05_003","05_004","05_005"],
["05_006","05_007","05_008","05_009","05_010"]
];
我可以通过使用以下方式输出数据来确认此数组正在运行:
for(var i = 0; i < d.length; i++) {
var item = d[i];
for(var j = 0; j < item.length; j++) {
console.log("item[" + i + "][" + j + "] = " + item[j]);
}
}
我想使用AJAX定义数组内容,并且使用此解决方案已经设法到目前为止:
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
这将获取数组元素
function getURL(url){
return $.ajax({
type: "GET",
url: url,
cache: false,
async: false
}).responseText;
}
//get the data back in the right format
var data = getURL("emoji.php?cat=emoticons_travel_places");
alert(data);
警报输出:
["05_001","05_002","05_003","05_004","05_005"],
["05_006","05_007","05_008","05_009","05_010"]
哪个好,但是我一直试图弄清楚如何将内容放到二维数组中。
当我尝试这个时:
我试过了:
var d = [
data
];
然后这个:
for(var i = 0; i < d.length; i++) {
var item = d[i];
for(var j = 0; j < item.length; j++) {
console.log("item[" + i + "][" + j + "] = " + item[j]);
}
}
输出:
item[0][0] = [
plugin.min.js:74 item[0][1] = "
plugin.min.js:74 item[0][2] = 0
plugin.min.js:74 item[0][3] = 5
plugin.min.js:74 item[0][4] = _
plugin.min.js:74 item[0][5] = 0
plugin.min.js:74 item[0][6] = 0
plugin.min.js:74 item[0][7] = 1
plugin.min.js:74 item[0][8] = "
plugin.min.js:74 item[0][9] = ,
plugin.min.js:74 item[0][10] = "
plugin.min.js:74 item[0][11] = 0
plugin.min.js:74 item[0][12] = 5
plugin.min.js:74 item[0][13] = _
plugin.min.js:74 item[0][14] = 0
plugin.min.js:74 item[0][15] = 0
plugin.min.js:74 item[0][16] = 2
plugin.min.js:74 item[0][17] = "
plugin.min.js:74 item[0][18] = ,
plugin.min.js:74 item[0][19] = "
plugin.min.js:74 item[0][20] = 0
plugin.min.js:74 item[0][21] = 5
plugin.min.js:74 item[0][22] = _
plugin.min.js:74 item[0][23] = 0
plugin.min.js:74 item[0][24] = 0
plugin.min.js:74 item[0][25] = 3
plugin.min.js:74 item[0][26] = "
plugin.min.js:74 item[0][27] = ,
plugin.min.js:74 item[0][28] = "
plugin.min.js:74 item[0][29] = 0
plugin.min.js:74 item[0][30] = 5
plugin.min.js:74 item[0][31] = _
plugin.min.js:74 item[0][32] = 0
plugin.min.js:74 item[0][33] = 0
plugin.min.js:74 item[0][34] = 4
plugin.min.js:74 item[0][35] = "
plugin.min.js:74 item[0][36] = ,
plugin.min.js:74 item[0][37] = "
plugin.min.js:74 item[0][38] = 0
plugin.min.js:74 item[0][39] = 5
plugin.min.js:74 item[0][40] = _
plugin.min.js:74 item[0][41] = 0
plugin.min.js:74 item[0][42] = 0
plugin.min.js:74 item[0][43] = 5
plugin.min.js:74 item[0][44] = "
plugin.min.js:74 item[0][45] = ]
plugin.min.js:74 item[0][46] = ,
plugin.min.js:74 item[0][47] =
plugin.min.js:74 item[0][48] = [
plugin.min.js:74 item[0][49] = "
plugin.min.js:74 item[0][50] = 0
plugin.min.js:74 item[0][51] = 5
plugin.min.js:74 item[0][52] = _
plugin.min.js:74 item[0][53] = 0
plugin.min.js:74 item[0][54] = 0
plugin.min.js:74 item[0][55] = 6
plugin.min.js:74 item[0][56] = "
plugin.min.js:74 item[0][57] = ,
plugin.min.js:74 item[0][58] = "
plugin.min.js:74 item[0][59] = 0
plugin.min.js:74 item[0][60] = 5
plugin.min.js:74 item[0][61] = _
plugin.min.js:74 item[0][62] = 0
plugin.min.js:74 item[0][63] = 0
plugin.min.js:74 item[0][64] = 7
plugin.min.js:74 item[0][65] = "
plugin.min.js:74 item[0][66] = ,
plugin.min.js:74 item[0][67] = "
plugin.min.js:74 item[0][68] = 0
plugin.min.js:74 item[0][69] = 5
plugin.min.js:74 item[0][70] = _
plugin.min.js:74 item[0][71] = 0
plugin.min.js:74 item[0][72] = 0
plugin.min.js:74 item[0][73] = 8
plugin.min.js:74 item[0][74] = "
plugin.min.js:74 item[0][75] = ,
plugin.min.js:74 item[0][76] = "
plugin.min.js:74 item[0][77] = 0
plugin.min.js:74 item[0][78] = 5
plugin.min.js:74 item[0][79] = _
plugin.min.js:74 item[0][80] = 0
plugin.min.js:74 item[0][81] = 0
plugin.min.js:74 item[0][82] = 9
plugin.min.js:74 item[0][83] = "
plugin.min.js:74 item[0][84] = ,
plugin.min.js:74 item[0][85] = "
plugin.min.js:74 item[0][86] = 0
plugin.min.js:74 item[0][87] = 5
plugin.min.js:74 item[0][88] = _
plugin.min.js:74 item[0][89] = 0
plugin.min.js:74 item[0][90] = 1
plugin.min.js:74 item[0][91] = 0
plugin.min.js:74 item[0][92] = "
plugin.min.js:74 item[0][93] = ]
plugin.min.js:74 item[0][94] =
因此,不是一个5乘2的数组,它似乎是一个1乘95的数组,因为AJAX调用返回的数据中每个字符都有一个元素。
如果它是相关的,这是“emoji.php”的PHP代码
<?php
$root="../../../";
include $root . 'config/init.php';
require $root . 'vendor/autoload.php';
$foo = "test";
$cat = $_GET['cat'];
$sql = "SELECT fld_unicode FROM xx_plugs_emojis WHERE fld_category = :cat LIMIT 10";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':cat', $_GET['cat']);
$stmt->execute();
$ct = $stmt->rowCount();
$str = NULL;
$max = $ct;
$row_count = 5;
$i = 1;
while ($row = $stmt->fetch()){
$emoji = $row['fld_unicode'];
$rem = $i % $row_count;
if ($rem === 1) {
$str .= "[";
}
if ($rem > 0 && $i != $max) {
$str .= "\"$emoji\",";
} else {
$str .= "\"$emoji\"";
}
if ($rem === 0 && $i < $max) {
$str .= "],\n";
}
if ($i === $max) {
$str .= "]\n";
}
$i++;
}
echo $str;
?>
答案 0 :(得分:1)
虽然您的硬编码数组是一个二维字符串数组,但AJAX调用返回的数据是一个纯字符串,并被视为一个。如果您想从服务器获取数组,则应使用JSON。
首先,PHP脚本返回的字符串不是有效的JSON。要使其有效,请使用其他方括号将其包装起来,以使其看起来像这样:
[["05_001","05_002","05_003","05_004","05_005"],
["05_006","05_007","05_008","05_009","05_010"]]
之后,您需要对函数getURL
进行小的更改:在JSON.parse()
方法中包装AJAX调用的结果:
function getURL(url){
return JSON.parse($.ajax({
type: "GET",
url: url,
cache: false,
async: false
}).responseText);
}
或者,您可以指定AJAX调用的返回类型:
function getURL(url){
return $.ajax({
type: "GET",
url: url,
dataType: 'json',
cache: false,
async: false
}).responseText;
}