求助!!php+ajax+ json 问题

WBOY
Release: 2016-06-23 14:39:14
Original
874 people have browsed it

本帖最后由 zl2727 于 2013-12-10 18:08:23 编辑

function gtou($s) { return iconv('gbk', 'utf-8', $s); }$proquery ="select * from pro where typeid='".$id."' order by corank asc"; $dsql->SetQuery($proquery); $dsql->Execute(); $a=array(); while($row = $dsql->GetArray()) { $a[]= array_map('gtou', $row); } $res = array( "status" => "1", "result" => $a, "moreUrl" => $moreurl, ); echo json_encode($res); 
Copy after login

现在前台 返回值是{"status":"1","result":[],"moreUrl":"http:\/\/wcn"} result 是空的
如果
echo json_encode($res);
换成下面的
//echo '{"status":"1","result":[{"id":"1","typeid":"111","goodstitle":"\u8865\u6c34\u6da6\u80a4\u818f \u771f\u7684\u5f88\u597d \u5f88\u8865\u6c34 \u76ae\u80a4\u6709\u5f39\u6027","goodsurl":"http:\/\/www.\/uploads\/allimg\/c131205\/13V23912150-114L_lit.jpg","brandtitle":"\u7f8e\u5b9d\u83b2","brandurl":"http:\/\/www.\/dsadasdas\/","imgurl":"http:\/\/www.\/sadas213\/","price":"59.9","corank":"1"},{"id":"2","typeid":"111","goodstitle":"\u8865\u6c34\u6da6\u80a4222","goodsurl":"http:\/\/www.\/uploads\/allimg\/c131205\/13V23912150-114L_lit.jpg","brandtitle":"\u7f8e222","brandurl":"http:\/\/www.\/dsadasdas\/","imgurl":"http:\/\/www.\/sadas213\/","price":"59.9","corank":"1"}],"moreUrl":"http:\/\/jianfei.\/shuiguo\/"}'; 就正确了 请大神们帮个忙 版主也麻烦您再次指导以下


回复讨论(解决方案)

用 var_dump 输出

print_r($a); 看看

Array ( [0] => Array ( [id] => 1 [goodstitle] => 琛ユ按娑???? ???寰?ソ 寰?ˉ姘? ?????脊?? [goodsurl] => http://www.ecoo.com.cn/uploads/allimg/c131205/13V23912150-114L_lit.jpg [brandtitle] => 缇???? [brandurl] => http://www.ecoo.com.cn/dsadasdas/ [imgurl] => http://www.ecoo.com.cn/sadas213/ [price] => 59.9 ) [1] => Array ( [id] => 2 [goodstitle] => 琛ユ按娑??222 [goodsurl] => http://www.ecoo.com.cn/uploads/allimg/c131205/13V23912150-114L_lit.jpg [brandtitle] => 缇?222 [brandurl] => http://www.ecoo.com.cn/dsadasdas/ [imgurl] => http://www.ecoo.com.cn/sadas213/ [price] => 59.9 ) )

这个是 var_dump 结果

array(2) { [0]=> array(7) { ["id"]=> string(1) "1" ["goodstitle"]=> string(54) "琛ユ按娑???? ???寰?ソ 寰?ˉ姘? ?????脊??" ["goodsurl"]=> string(70) "http://www.ecoo.com.cn/uploads/allimg/c131205/13V23912150-114L_lit.jpg" ["brandtitle"]=> string(9) "缇????" ["brandurl"]=> string(33) "http://www.ecoo.com.cn/dsadasdas/" ["imgurl"]=> string(32) "http://www.ecoo.com.cn/sadas213/" ["price"]=> string(4) "59.9" } [1]=> array(7) { ["id"]=> string(1) "2" ["goodstitle"]=> string(15) "琛ユ按娑??222" ["goodsurl"]=> string(70) "http://www.ecoo.com.cn/uploads/allimg/c131205/13V23912150-114L_lit.jpg" ["brandtitle"]=> string(6) "缇?222" ["brandurl"]=> string(33) "http://www.ecoo.com.cn/dsadasdas/" ["imgurl"]=> string(32) "http://www.ecoo.com.cn/sadas213/" ["price"]=> string(4) "59.9" } }

不会的,$a 不为空,就不会输出空数组

版主 您可以加下我吗? 我给您发测试连接 您看看

我把 $a 换成

$a = array ( array( "id" => "1102444","title"=>"事实上","goodstitle"=>"事实上","price"=>"429.0","imgurl"=>".jpg","goodsurl"=>"/","brandtitle"=>"44444","brandurl"=>"_share/" ), array( "id" => "1102444","title"=>"赌东道的","goodstitle"=>"赌东道的","price"=>"429.0","imgurl"=>"0.jpg","goodsurl"=>"2444/","brandtitle"=>"7777","brandurl"=>"share/" ) );
Copy after login
也是正确的 前台可以接到值 result不是空

是不是数据库的问题 您说的 在读数据的时候也可以转 怎么转UTF-8? 我找了好久
我的表是GBK的表 我新建了一个UTF-8的表也不行

$a 在函数中,你调用了吗。函数的return 也不对吧?

谢谢 大神们的支持 由于小弟疏忽

在AJAX请求时 路径写错

其他啥问题都没有

结贴!!!

function gtou($s) { return iconv('gbk', 'utf-8', $s); }$a = array ( array( "id" => "1102444","title"=>"事实上","goodstitle"=>"事实上","price"=>"429.0","imgurl"=>".jpg","goodsurl"=>"/","brandtitle"=>"44444","brandurl"=>"_share/" ), array( "id" => "1102444","title"=>"赌东道的","goodstitle"=>"赌东道的","price"=>"429.0","imgurl"=>"0.jpg","goodsurl"=>"2444/","brandtitle"=>"7777","brandurl"=>"share/" ) );foreach($a as $r) $t[] = array_map('gtou', $r);$moreurl = 'x';$res = array( "status" => "1", "result" => $t, "moreUrl" => $moreurl, ); echo json_encode($res);
Copy after login
{"status":"1","result":[{"id":"1102444","title":"\u4e8b\u5b9e\u4e0a","goodstitle":"\u4e8b\u5b9e\u4e0a","price":"429.0","imgurl":".jpg","goodsurl":"\/","brandtitle":"44444","brandurl":"_share\/"},{"id":"1102444","title":"\u8d4c\u4e1c\u9053\u7684","goodstitle":"\u8d4c\u4e1c\u9053\u7684","price":"429.0","imgurl":"0.jpg","goodsurl":"2444\/","brandtitle":"7777","brandurl":"share\/"}],"moreUrl":"x"}

不会有问题的,检查你每次的查询条件是否正确!
没有查询到,自然就是空数组
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template