繁体   English   中英

NaN对Ajax请求返回数据

[英]NaN on ajax request returned data

在api的php端,我有这个:

$res = $db->query("SELECT count(upVote) FROM tbl_post_likers WHERE post_id='$pnum' AND upVote='true'");
$row = $res->fetchArray();
if($row[0] > 0){
    $upVotes = $row[0];
    $res = $db->query("SELECT count(downVote) FROM tbl_post_likers WHERE post_id='$pnum' AND downVote='true'");
    $row = $res->fetchArray();
    $downVotes = $row[0];

    $tot = $upVotes + $downVotes;
    $upVotesPer = ($upVotes * 100) / $tot;
    $downVotesPer = ($downVotes * 100) / $tot;
    return $upVotesPer - $downVotesPer;
}else if ($row[0] == 0){
    return 0;
}

我试图从api作为数组返回更多数据。 在javascript端,当我JSON.parse返回的数据时,我正确地获得了所有数据,如下所示:例如:

0: [...],
1: [...],
2: 0

零是return 0; 从php方面,问题是当我console.log第三个属性undefined时,或者当我parseInt它时,它返回NaN而返回数据的第三个属性为零。

这是实际的console.log

0 : "{"0":2,"id":2,"1":"0.jpg","image":"0.jpg","2":"Proj 1","title":"Proj 1","3":"some description here about the course!","desc":"some description here about the course!","4":"sinawic","teacher":"sinawic","5":0,"sell_count":0,"6":"1997.05.05","date_released":"1997.05.05","7":97,"preview":97,"8":0,"likes":0,"9":0,"post_point":0,"10":0,"post_cash":0}" 1 : "["jquery","vue"]" 2 : "[{"lesson_num":1,"topic_name":"pre"}]" 3 : "{"0":"sinawic","username":"sinawic","1":"web design, React, VUE, javascript","fields_name":"web design, React, VUE, javascript","2":"this is all about me, oke? call me maybe","description":"this is all about me, oke? call me maybe","3":"85%, 53%, 36%, 74%, 17%","ranks":"85%, 53%, 36%, 74%, 17%"}" 5 : 0

这是对api的数据echo显:

{"0":"{\\"0\\":2,\\"id\\":2,\\"1\\":\\"0.jpg\\",\\"image\\":\\"0.jpg\\",\\"2\\":\\"Proj 1\\",\\"title\\":\\"Proj 1\\",\\"3\\":\\"some description here about the course!\\",\\"desc\\":\\"some description here about the course!\\",\\"4\\":\\"sinawic\\",\\"teacher\\":\\"sinawic\\",\\"5\\":0,\\"sell_count\\":0,\\"6\\":\\"1997.05.05\\",\\"date_released\\":\\"1997.05.05\\",\\"7\\":98,\\"preview\\":98,\\"8\\":0,\\"likes\\":0,\\"9\\":0,\\"post_point\\":0,\\"10\\":0,\\"post_cash\\":0}","1":"[\\"jquery\\",\\"vue\\"]","2":"[{\\"lesson_num\\":1,\\"topic_name\\":\\"pre\\"}]","3":"{\\"0\\":\\"sinawic\\",\\"username\\":\\"sinawic\\",\\"1\\":\\"web design, React, VUE, javascript\\",\\"fields_name\\":\\"web design, React, VUE, javascript\\",\\"2\\":\\"this is all about me, oke? call me maybe\\",\\"description\\":\\"this is all about me, oke? call me maybe\\",\\"3\\":\\"85%, 53%, 36%, 74%, 17%\\",\\"ranks\\":\\"85%, 53%, 36%, 74%, 17%\\"}","5":0}

我怎么可能解决它。

您的代码正在尝试访问Object.keys数组中索引5处的项目,该项目将是第6个项目。 但是,密钥编号之间有空隙-不包括4。

因此,具有键“ 5”的对象实际上是键列表中的第五项,因此可以在索引4处访问-因为键数组与所有JS数组一样都是从零开始的。 没有第六项-该对象只有5个键。

我认为因为对象键是数字,所以您在键的名称及其在键列表中的位置之间感到困惑。

在此(可运行)示例中,观察最后两个console.logs之间的区别:

 var allData = { 0:{"0":2,"id":2,"1":"0.jpg","image":"0.jpg","2":"Proj 1","title":"Proj 1","3":"some description here about the course!","desc":"some description here about the course!","4":"sinawic","teacher":"sinawic","5":0,"sell_count":0,"6":"1997.05.05","date_released":"1997.05.05","7":97,"preview":97,"8":0,"likes":0,"9":0,"post_point":0,"10":0,"post_cash":0}, 1:["jquery","vue"], 2:[{"lesson_num":1,"topic_name":"pre"}], 3:{"0":"sinawic","username":"sinawic","1":"web design, React, VUE, javascript","fields_name":"web design, React, VUE, javascript","2":"this is all about me, oke? call me maybe","description":"this is all about me, oke? call me maybe","3":"85%, 53%, 36%, 74%, 17%","ranks":"85%, 53%, 36%, 74%, 17%"}, 5:0 }; console.log(Object.keys(allData)); console.log(allData[Object.keys(allData)[5]]); console.log(allData[Object.keys(allData)[4]]); 

但是,您可以大大简化此过程。 如果您已经知道想要键为“ 5”的项目,则可以直接访问它:

 var allData = { 0:{"0":2,"id":2,"1":"0.jpg","image":"0.jpg","2":"Proj 1","title":"Proj 1","3":"some description here about the course!","desc":"some description here about the course!","4":"sinawic","teacher":"sinawic","5":0,"sell_count":0,"6":"1997.05.05","date_released":"1997.05.05","7":97,"preview":97,"8":0,"likes":0,"9":0,"post_point":0,"10":0,"post_cash":0}, 1:["jquery","vue"], 2:[{"lesson_num":1,"topic_name":"pre"}], 3:{"0":"sinawic","username":"sinawic","1":"web design, React, VUE, javascript","fields_name":"web design, React, VUE, javascript","2":"this is all about me, oke? call me maybe","description":"this is all about me, oke? call me maybe","3":"85%, 53%, 36%, 74%, 17%","ranks":"85%, 53%, 36%, 74%, 17%"}, 5:0 }; console.log(allData[5]); 

您一直在用数组索引混合对象,这必须对您allData[5]allData[5]将返回它的值。

我创建了两个jsfiddles:

一个没有5键的数组(返回未定义) jsfiddle没有5 // allData = {0:'abc',1:'def',2:'ghi',3:'dlsdds',4:'dasdas'}; var ret = allData[Object.keys(allData)[5]]; console.log(ret); // allData = {0:'abc',1:'def',2:'ghi',3:'dlsdds',4:'dasdas'}; var ret = allData[Object.keys(allData)[5]]; console.log(ret);

第二个数组中有5个kry(返回实际值) jsfiddle带有5 // allData = {0:'abc',1:'def',2:'ghi',3:'dlsdds',4:'dasdas',5:'sddsds'}; var ret = allData[Object.keys(allData)[5]]; console.log(ret); // allData = {0:'abc',1:'def',2:'ghi',3:'dlsdds',4:'dasdas',5:'sddsds'}; var ret = allData[Object.keys(allData)[5]]; console.log(ret);

比较一下就可以看到区别

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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