微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 为什么responseText返回空白?

我有一个数据库表,其字段包含位置的纬度和经度坐标.我想使用数据库中的信息为Google Map视图创建标记.

我已经将查询函数作为

function getCords(){
  $link = connectDB();
  $query = "SELECT * FROM tour";
  $results = MysqLi_query($link, $query);

  $jsonArray = array();
  while ($row = MysqLi_fetch_assoc($results)){
    $jsonArray[] = array('fileName' => $row['FileName'], 'lat' => $row['Lat'], 'lon' => $row['Lon']);

  }

return json_encode($jsonArray);
}

当我从PHP页面调用函数时,它返回通常的JSON格式.

我的问题是执行ajax查询.我在上面的PHP脚本文件中有查询功能,该文件包含六个左右的实用程序函数,用于控制登录,注销,注册等.为了通过jquery查询数据库,我试过了

var request = $.ajax({
  type:"GET",
  url: "includes/PHPscripts.PHP?action=cords",
  type: "json"
});

var response = request.responseText;

我的问题是响应总是空的.这是由于URL的形成还是由于其他原因造成的?

解决方法:

   $.ajax({
      type:"GET",
      url: "includes/PHPscripts.PHP?action=cords",
      dataType: 'json', // necessary, because you're sending json from server
      success: function(response) {  // response will catch within success function
        console.log(response);
      }
    });

要么

   var request = $.ajax({
      type:"GET",
      url: "includes/PHPscripts.PHP?action=cords",
      dataType: 'json', // necessary, because you're sending json from server
    }).done(function(response) {
       console.log(response);
    });

注意

而不是返回json_encode($jsonArray);,使用echo json_encode($jsonArray);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐