2016-09-29 87 views
-3

我不知道爲什麼代碼不工作,它應該編碼爲json? 當我打電話從應用這個PHP代碼鏈接的Android工作室它不工作並顯示錯誤消息,它顯示這個代碼:第1行json解析錯誤:< html >< body >

<html> <body> < script type = "text/javascript" 
src = "/aes.js" > < /script><script>function toNumbers(d){var e=[];d.replace(/ (..)/g, 
function(d) { 
    e.push(parseInt(d, 16)) 
}); 
return e 
} 
function toHex() { 
for (var d = [], d = 1 == arguments.length && arguments[0].constructor == Array ? arguments[0] : arguments, e = "", f = 0; f < d.length; f++) e += (16 > d[f] ? "0" : "") + d[f].toString(16); 
return e.toLowerCase() 
} 
var a = toNumbers("f655ba9d09a112d4968c63579db590b4"), 
b = toNumbers("98344c2eee86c3994890592585b49f80"), 
c = toNumbers("fafd97787619720460a70e34d00aedb4"); 
document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; 
location.href = "http://gmarket.byethost5.com/json_getS.php?i=2"; 

也jsonlint網站顯示同上述當測試PHP URL並返回該錯誤:

第1行

解析錯誤:< HTML> <體> < ^期待 'STRING', 'NUMBER', 'NULL', 'TRUE', '假', '{', '[' ,得到'undefined')

這個PHP鏈接:http://gmarket.byethost5.com/json_getS.php

這個PHP代碼,我用:

<?php 
$host= "sql201.byethost5.com"; 
$user = "xxxxxxx"; 
$pass = "xxxxxxx"; 
$db = "xxxxxxx"; 

$sql="select * from customer"; 

$con = mysqli_connect($host,$user,$pass,$db); 
$result= mysqli_query($con,$sql); 

$response = array(); 

while ($row = mysqli_fetch_array($result)) 
{ 

array_push($response,array("name"=>$row[0],"phone"=>$row[1])); 
} 
echo json_encode(array("customers"=>$response)); 

mysqli_close($con); 
?> 
+0

URL返回json。看起來問題出在Android代碼 – Jens

+0

'$ response = array(); ($ row = mysqli_fetch_array($ result)) { $ response ['customers'] [] = array(「name」=> $ row [0],「phone」=> $ row [1] ); } echo json_encode($ response);' –

回答

0

您的JSON是正確的,問題是你的返回HTML頁面,你不告訴內容是Json的瀏覽器。

使用

header('Content-Type: application/json'); 
echo json_encode($response); 
+0

我在應用程序中使用它,沒有任何更改,同樣的問題>< – Nawaf

0

我用另一個虛擬主機域名(000webhost的)及其而不相同的代碼的任何問題的工作,我覺得從(php空間)不是從代碼的問題。

相關問題