解决方法 Uncaught SyntaxError: Unexpected token < in JSON at position 0
来源:互联网 发布:linked in 知乎 编辑:程序博客网 时间:2024/05/24 01:39
win7 操作系统 php5.4和phpstorm8.3 (php集成开发环境,可以编辑调试)开发遇到问题
$(function(){
var g_table = $("table.data");
var init_data_url = "data.php?action=init_data_list";
$.get(init_data_url,function(data){
var row_items = $.parseJSON(data); //报错如下
for( var i = 0 , j = row_items.length ; i < j ; i++) {
var data_dom = create_row(row_items[i]);
g_table.append(data_dom);
}
});
报错:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
(翻译:不能捕获的语法错误:json数据位置0处发现不能识别的标记<)
at Function.parse [as parseJSON] (<anonymous>)at Object.success (course.js:5)
at fire (jquery-3.1.1.js:3305)
at Object.fireWith [as resolveWith] (jquery-3.1.1.js:3435)
at done (jquery-3.1.1.js:9242)
at XMLHttpRequest.<anonymous> (jquery-3.1.1.js:9484)
原因:json格式错误
返回数据 语句用echo json_encode($data,true);但是用了echo json_decode($data);
由于函数使用不对,返回的data不是json数据,第一个字符是“ <”,所以报错Unexpected token < in JSON at position 0(翻译:json数据位置0处发现不能识别的标记<)。
json教程见:http://www.w3school.com.cn/json/index.asp
json样例如下:
{"employees": [{ "firstName":"Bill" , "lastName":"Gates" },{ "firstName":"George" , "lastName":"Bush" },{ "firstName":"Thomas" , "lastName":"Carter" }]}
解决办法:
修改为echo json_encode($data,true);
如果不是该问题,请检查php文件给返回的data数据,是否标准json格式。如果php文件中有var_dump、echo、注释等,都会对返回的json数据有影响。
一般报该错误,基本是数据json格式问题,请在浏览器里面调试检查。
- 解决方法 Uncaught SyntaxError: Unexpected token < in JSON at position 0
- Uncaught SyntaxError: Unexpected token < in JSON at position 0
- Uncaught SyntaxError: Unexpected token ] in JSON at position 问题
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- Atitit Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0
- primefaces 推送服务报Uncaught SyntaxError: Unexpected token < in JSON at position 0
- 【已解决】Uncaught SyntaxError: Unexpected token in JSON at position 0
- Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
- 【已解决】Uncaught SyntaxError: Unexpected token o in JSON at position 1
- Javaweb中 Uncaught SyntaxError: Unexpected token o in JSON at position 1
- VM356:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
- VM356:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
- 代码中出现:Uncaught SyntaxError: Unexpected token o in JSON at position 1
- js获取富文本编辑器的值之后报错Uncaught SyntaxError: Unexpected token 发 in JSON at position 0
- SyntaxError: Unexpected token s in JSON at position 111518
- SyntaxError: Unexpected token s in JSON at position 1
- RHEL 7.0 配置本地YUM源
- Textiew常用设置
- 实现Python中xrange函数
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- UML用户指南第二版-读书笔记-第1章-为什么要建模
- 解决方法 Uncaught SyntaxError: Unexpected token < in JSON at position 0
- HTML-多媒体标签
- ABAP修改请求号
- 5 MATLAB参数估计与假设检验-参数估计
- 对IOC和DI的通俗理解
- java中数据库连接池框架c3p0和dbcp的区别
- IPC基础概念介绍
- 5个步骤学会Win2008 Server Core操作
- base64编码的作用 、为何使用base64编码、base64编码使用场景