$.ajax 调用不适用于数据类型:'json'



如果这听起来很愚蠢,请原谅,但我已经做了很长时间了,无法解决问题。

所以我想做的是:

--对php脚本进行ajax调用,该脚本查询mongodb集合中的一些文档

--这个ajax调用返回一个json并在浏览器上呈现它。

我遇到的问题是第一步本身。

这是我的ajax调用:

        $.ajax({
                type :'get',
                url: "get_data.php",
                dataType : 'json',
                success: function(msg){
                    alert(msg);
                }
            });

这是php代码:

        $n =  new MongoClient();    
        $dbname = "wsd";    
        $db = $n->$dbname; //get the collections.. 
        $collection = $db->raretweets; 
        $cursor = $collection->find();
        header('Content-type: application/json');
        echo json_encode($cursor);

我怀疑问题可能出在$cursor对象上,所以我尝试了以下基本内容

         $data = 3;
         header("Content-Type: application/json", true);
        /* Return JSON */
        echo json_encode($data);

即使这样,我也会在firebug的控制台日志中出现错误"未找到元素"。我想知道是否有人能向我暗示这个问题。

如果你尝试一些非常基本的东西,比如

$.get(url, function(data){
  if(data!='Error') {
    alert(data);
  }

在PHP 中

$data = 3;
if($data) {
  echo number_format($data);
} else {
  die('Error');
}

这会给你一个结果吗?

最新更新