如果接收到数组,如何在jQuery中解析来自php的数据&;对象
PHP: JS: 但是如果从PHP收到GoodIMG+BadIMG没有在#fast-reply_text区域插入任何内容:如果接收到数组,如何在jQuery中解析来自php的数据&;对象,php,jquery,ajax,parsing,echo,Php,Jquery,Ajax,Parsing,Echo,PHP: JS: 但是如果从PHP收到GoodIMG+BadIMG没有在#fast-reply_text区域插入任何内容: console.log(response.BadIMG); undefined console.log(response.GoodIMG); Array [ "1_Good.jpg", "2_Good.jpg" ] console.log(response.BadIMG); Array [ "1_Bad.jpg"
console.log(response.BadIMG);
undefined
console.log(response.GoodIMG);
Array [ "1_Good.jpg", "2_Good.jpg" ]
console.log(response.BadIMG);
Array [ "1_Bad.jpg", "2_Bad.jpg" ]
console.log(response.GoodIMG);
Object { 2: "1_Good.jpg", 3: "2_Good.jpg" }
如果仅从PHP收到BadIMG:
console.log(response.BadIMG);
undefined
console.log(response.GoodIMG);
Array [ "1_Good.jpg", "2_Good.jpg" ]
console.log(response.BadIMG);
Array [ "1_Bad.jpg", "2_Bad.jpg" ]
console.log(response.GoodIMG);
Object { 2: "1_Good.jpg", 3: "2_Good.jpg" }
如果收到BadIMG以及GoodIMG并在编辑器中添加GoodIMG链接,如何解析此数据?
对于BadIMG我将添加警报,例如:从(统计所有图像)未上载的图像(统计BadIMG的数量或名称)。您可以在PHP中删除数组中的
$I
索引
console.log(response.BadIMG);
Array [ "1_Bad.jpg", "2_Bad.jpg" ]
console.log(response.GoodIMG);
undefined //With error:
Uncaught TypeError: can't access property "length", response.GoodIMG is undefined
在这种情况下,两者都是数组,而不是对象
此外,在JS中,您应该测试变量是否已定义
$files_arr['BadIMG'][] = $fileName;
$files_arr['GoodIMG'][] = $path;
成功:功能(响应){
if(type of response.GoodIMG!=“未定义”){
对于(变量i=0;i
我认为创建$files\u arr['GoodIMG']
是有条件的。在每种情况下都添加它,或者检查它是否在JSif(response.GoodIMG){for(…)
中定义。但是如果收到GoodIMG和BadIMG,为什么不在所有情况下返回相同的结构以避免此类问题?
success: function(response) {
if (typeof response.GoodIMG !== "undefined") {
for (var i = 0; i < response.GoodIMG.length; i++) {
var src = response.GoodIMG[i];
$('#fast-reply_textarea').focus().val($('#fast-reply_textarea').val() + '\n[img]https://url/' + src + '[/img]\n');
}
}
}