求助!!php+ajax+ json 問題

來源:互聯網
上載者:User
本帖最後由 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"}

不會有問題的,檢查你每次的查詢條件是否正確!
沒有查詢到,自然就是空數組
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.