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

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

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

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
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在js中mouseover和 mouseenter的区别浅析(0个评论)
    • uniapp调用手机实现打电话录音功能示例代码(0个评论)
    • 在uniapp开发微信小程序中图片大小显示不正常解决方法(0个评论)
    • 在vue2中如何实现v-model示例(0个评论)
    • js + Bolb实现文件流下载csv文件示例代码(0个评论)
    • 近期文章
    • 在Laravel应用程序如何减少代码重复编写(0个评论)
    • 在laravel项目中提高安全性方式推荐:CSP内容安全策略(0个评论)
    • 在go语言中从值中获取常量名称代码示例(0个评论)
    • 在go语言中如何通过名称获得结构字段和值代码示例(0个评论)
    • 在go语言中用JQuery + html2canvas实现拍摄浏览器的屏幕截图示例(0个评论)
    • 人生感悟分享:讲一个大学毕业生到社畜老狗的蜕变心路历程(0个评论)
    • laravel9框架报错Target class... does not exist解决方式(0个评论)
    • Laravel 9.48版本发布(0个评论)
    • Meta高级工程师现身说法:程序员干得越久,代码写得越少?(0个评论)
    • 本站zongscan祝大家除夕快乐,2023有奔头(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 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
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    Top

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

    侯体宗的博客