ajax调用返回php接口返回json数据的方法(必看篇)


Posted in PHP onMay 05, 2017

php代码如下:

<?php

  header('Content-Type: application/json');
  header('Content-Type: text/html;charset=utf-8');

  $email = $_GET['email'];

  $user = [];

  $conn = @mysql_connect("localhost","Test","123456") or die("Failed in connecting database");
  mysql_select_db("Test",$conn);
  mysql_query("set names 'UTF-8'");
  $query = "select * from UserInformation where email = '".$email."'";
  $result = mysql_query($query);
  if (null == ($row = mysql_fetch_array($result))) {
    echo $_GET['callback']."(no such user)";
  } else {
    $user['email'] = $email;
    $user['nickname'] = $row['nickname'];
    $user['portrait'] = $row['portrait'];
    echo $_GET['callback']."(".json_encode($user).")";
  }

?>

js代码如下:

<script>
    $.ajax({
      url: "http://test.localhost/UserInterfaceForChatroom/UserInformation.php?email=pshuyue@gmail.com",
      type: "GET",
      dataType: 'jsonp',
      //      crossDomain: true,
      success: function (result) {
        //        data = $.parseJSON(result);
        //        alert(data.nickname);
        alert(result.nickname);
      }
    });
  </script>

其中遇到了两个问题:

1、第一个问题:

Uncaught SyntaxError: Unexpected token :

解决方案如下:

This has just happened to me, and the reason was none of the reasons above. I was using the jQuery command getJSON and adding callback=? to use JSONP (as I needed to go cross-domain), and returning the JSON code {"foo":"bar"} and getting the error.

This is because I should have included the callback data, something like jQuery17209314005577471107_1335958194322({"foo":"bar"})

Here is the PHP code I used to achieve this, which degrades if JSON (without a callback) is used:

$ret['foo'] = "bar";
finish();

function finish() {
  header("content-type:application/json");
  if ($_GET['callback']) {
    print $_GET['callback']."(";
  }
  print json_encode($GLOBALS['ret']);
  if ($_GET['callback']) {
    print ")";
  }
  exit; 
}

Hopefully that will help someone in the future.

2、第二个问题:

解析json数据。从上面的javascript中可以看到,我没有使用jquery.parseJSON()这些方法,开始使用这些方法,但是总是会报

VM219:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1的错误,后来不用jquery.parseJSON()这个方法,反而一切正常。不知为何。

以上这篇ajax调用返回php接口返回json数据的方法(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
文件系统基本操作类
Nov 23 PHP
PHP 数组实例说明
Aug 18 PHP
基于HTTP长连接的&quot;服务器推&quot;技术的php 简易聊天室
Oct 31 PHP
PHP判断图片格式的七种方法小结
Jun 03 PHP
php中通过curl检测页面是否被百度收录
Sep 27 PHP
php文件上传的例子及参数详解
Dec 12 PHP
php过滤敏感词的示例
Mar 31 PHP
php通过array_push()函数添加多个变量到数组末尾的方法
Mar 18 PHP
php恢复数组的key为数字序列的方法
Apr 28 PHP
php计算整个目录大小的方法
Jun 19 PHP
PHP数学运算函数大汇总(经典值得收藏)
Apr 01 PHP
浅析PHP中的闭包和匿名函数
Dec 25 PHP
ThinkPHP 3.2.2实现事务操作的方法
May 05 #PHP
PHP实现Session入库/存入redis的方法
May 04 #PHP
ThinkPHP中Widget扩展的两种写法及调用方法详解
May 04 #PHP
PHP+jQuery实现滚屏无刷新动态加载数据功能详解
May 04 #PHP
PHP调用Mailgun发送邮件的方法
May 04 #PHP
PHP实现图片的等比缩放和Logo水印功能示例
May 04 #PHP
Yii2数据库操作常用方法小结
May 04 #PHP
You might like
php 无限极分类
2008/03/27 PHP
PHP中使用GD库创建圆形饼图的例子
2014/11/19 PHP
PHP实现对xml的增删改查操作案例分析
2017/05/19 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
2017/08/31 PHP
Prototype 学习 工具函数学习($w,$F方法)
2009/07/12 Javascript
jQuery EasyUI API 中文文档 - Documentation 文档
2011/09/29 Javascript
node.js中的fs.rename方法使用说明
2014/12/16 Javascript
JS组件Bootstrap Table布局详解
2016/05/27 Javascript
有关jQuery中parent()和siblings()的小问题
2016/06/01 Javascript
修改ligerui 默认确认按钮的方法
2016/12/27 Javascript
vue-lazyload图片延迟加载插件的实例讲解
2018/02/09 Javascript
jQuery简单实现根据日期计算星期几的方法
2019/01/09 jQuery
uni-app从安装到卸载的入门教程
2020/05/15 Javascript
解决vue中el-tab-pane切换的问题
2020/07/19 Javascript
Django使用paginator插件实现翻页功能的实例
2018/10/24 Python
Django组件之cookie与session的使用方法
2019/01/10 Python
python模块hashlib(加密服务)知识点讲解
2019/11/25 Python
python的range和linspace使用详解
2019/11/27 Python
matplotlib 三维图表绘制方法简介
2020/09/20 Python
详解HTML5 data-* 自定义属性
2018/01/24 HTML / CSS
Gina Bacconi官网:吉娜贝康尼连衣裙和礼服
2018/04/24 全球购物
美国狗旅行和户外用品领先供应商:kurgo
2020/08/18 全球购物
入党自我鉴定范文
2013/10/04 职场文书
大学学年自我鉴定
2013/10/28 职场文书
承办会议欢迎词
2014/01/17 职场文书
企业办公室岗位职责
2014/03/12 职场文书
农村面貌改造提升实施方案
2014/03/18 职场文书
仓库文员岗位职责
2014/04/06 职场文书
书香家庭事迹材料
2014/05/09 职场文书
投资建议书模板
2014/05/12 职场文书
高三语文复习计划
2015/01/19 职场文书
鲁迅故居导游词
2015/02/05 职场文书
2015年教师节感言
2015/08/03 职场文书
Redis 彻底禁用RDB持久化操作
2021/07/09 Redis
python之PySide2安装使用及QT Designer UI设计案例教程
2021/07/26 Python
面试被问select......for update会锁表还是锁行
2021/11/11 MySQL