1

我有一个创建 json 的 php (history.php)

$i=1;
 $q=mysql_query("select * from participants where phone='".mysql_real_escape_string($_GET['phone'])."' limit 10");
 while($rs=mysql_fetch_array($q)){
      $response[$i] = $rs['code'];
$i++;   
    }
    print json_encode($response);
    exit;

在js中我访问这个文件:

var req=$.get("history.php", { phone: "" + phone + ""},

                        function(data) {
        //data="1":"code1","2":"code2","3":"code3","4":"code4","5":"code5"};
                            var msg = "";
                            for(i=1;i<=5;i++){
                                msg+= "<li>"+data[i];
                            }
                            $(form_message).html(msg);

                        })

执行此代码后,我的输出是

  • "
  • 1
  • "
  • "
  • 这意味着“数据”不是作为数组传递的。它像字符串一样传递。但是,如果我取消注释 js 中的数据变量,一切正常。输出是:

  • 代码1
  • 代码2
  • 代码3
  • 代码4
  • 代码5
  • 你能告诉我从 php.ini 传递数据时我做错了什么吗?

    提前致谢

    4

    1 回答 1

    2

    As in your case returned content is interpreted as text, not as json, you need to use $.getJSON instead of $.get:

    var req=$.getJSON("history.php", { phone: "" + phone + ""}, function(data) {
        var msg = "";
        for(var i in data){
            msg+= "<li>"+data[i]+"</li>";
        }
        $(form_message).html(msg);
    });
    
    于 2012-06-16T07:14:27.903 回答