侯体宗的博客
  • 首页
  • Hyperf2.1版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

前端  /  管理员 发布于 4年前   427

php代码如下:


<?php  

header('Content-Type: application/json');  

header('Content-Type: text/html;');  

$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"})</p><p>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()这个方法,反而一切正常。不知为何。                                                                  



  • 上一条:
    mysql sql语句 同步,替换
    下一条:
    easyui datebox 开始/结束时间 动态互相限制
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • TP(3/5)
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • uni-app在详情页中给电话信息添加点击拨打电话的功能按钮(0个评论)
    • uni-app开发小程序搜索功能及添加搜索历史记录、添加热门搜索关键词功能demo(0个评论)
    • uni-app开发小程序使用uni.getLocation()实现页面即时获取当前定位信息demo(1个评论)
    • uni-app开发小程序实现列表页条件筛选显示功能demo(0个评论)
    • vue+elementui实现百度地图api滑上列表显示位置信息(0个评论)
    • 近期文章
    • laravel中轻量化的钱包实现方案:Laravel Wallet(0个评论)
    • mysql性能优化之8种常见SQL错误用法(0个评论)
    • Laravel 9.13版本发布(0个评论)
    • beego+GeoLite2免费数据库获取ip地址经纬度等定位归属信息(0个评论)
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • PHP + Memcache实现简单的统计当前在线人数功能(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • go语言中使用Signbit()函数判断一个整数是正数或负数(0个评论)
    • 删库跑路之一链家程序员删除公司9TB数据被判7年,望各大码农警之!(0个评论)
    • Laravel角色和权限:拦截器Gates和策略Policies的解释(0个评论)
    • 近期评论
    • 阿凡达123 在

      golang 怎么做热更新中评论 也可以看看这个:https://github.com/edwingeng/hot..
    • 博主 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..
    • GGGGGGGGG 在

      layui框架常用输入框介绍中评论 写的很好解决问题..
    • 路过的靓仔 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..
    • 激光豆芽 在

      为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客