donglanzhan7151 2015-08-21 09:05
浏览 58

我的PHP没有返回JSON

I try to learn basic PHP to connect between my server and android but I have a problem with my basic PHP it return html instead JSON.

this is my PHP code:

<?php
mysql_connect("sql100.0fees.us","*********","*******");
mysql_select_db("db shlomi");
$sql=mysql_query("select * from pru");
$output=array();
while($row=mysql_fetch_assoc($sql)) {
   $output[]= array(
       'name' => $row['name'],
     'quantity' => $row['quantity']);
}
echo(json_encode($output));
mysql_close();
?>

when I run this in my browser I receive [{"name":"10","quantity":"50"},{"name":"10","quantity":"50"}]

but in my app or in jsonlint.com I receive error like html

<html><body><scripttype="text/javascript"src="/aes.js"></script><script>functiontoNumbers(d){
    vare=[

    ];d.replace(/(..)/g,
    function(d){
        e.push(parseInt(d,
        16))
    });returne
}functiontoHex(){
    for(vard=[

    ],
    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);returne.toLowerCase()
}vara=toNumbers("f655ba9d09a112d4968c63579db590b4"),
b=toNumbers("98344c2eee86c3994890592585b49f80"),
c=toNumbers("68c0fc9ff46cfabf092145e84011ee13");document.cookie="__test="+toHex(slowAES.decrypt(c,
2,
a,
b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://ranva.0fees.us/query.php?ckattempt=1";</script><noscript>ThissiterequiresJavascripttowork,
pleaseenableJavascriptinyourbrowseroruseabrowserwithJavascriptsupport</noscript></body></html>

so I understand that my problem is in the PHP because it didn't return JSON.

  • 写回答

2条回答 默认 最新

  • doubi5127 2015-08-21 09:46
    关注

    The response generated by your script is JSON but it is advertised as HTML in the header of the response because the default Content-Type returned by PHP scripts is text/html.

    If your script generates anything else than HTML it has to mention this in the response by sending the correct Content-Type using the PHP function header().

    The correct content type for JSON content is application/json. Use the function header() to set the correct content type before sending any content to the browser.

    header('Content-Type: application/json');
    echo(json_encode($output));
    

    Very important!

    The mysql PHP extension is deprecated since PHP 5.5 and it was removed from PHP 7.
    Forget it!

    Learn and use either mysqli or PDO instead.

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面