> 백엔드 개발 > PHP 튜토리얼 > 求助!!php+ajax+ json 问题

求助!!php+ajax+ json 问题

WBOY
풀어 주다: 2016-06-23 14:39:14
원래의
898명이 탐색했습니다.

本帖最后由 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); 
로그인 후 복사

现在前台 返回值是{"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/" ) );
로그인 후 복사
也是正确的 前台可以接到值 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);
로그인 후 복사
{"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"}

不会有问题的,检查你每次的查询条件是否正确!
没有查询到,自然就是空数组
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿