jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
我在使用$.parseJSON解析后台返回的JSON的数据时,出现了这样的错误,我还以为返回的JSON格式出现了错误,因为JSON要求格式非常严格。最后发现JSON格式没有太明显的格式错误,我使用fastJSON来生成的JSON格式数据,原来是因为数据已经是一个JavaScript对象了,所以在进行解析就会出错了
我直接将这段数据alert出来,并使用typeof检验其类型,发现是一个Object,这就证明了数据已成为了JavaScript对象了。所以,我直接使用(不用什么parseJSON解析了)这段数据进行相应的处理就不会出错
$.ajax({
url : 'commentAction_showComment',
type : 'POST',
data:{
titleid: $(comment_this).attr('data-id'),
currPage : currPage,
},
beforeSend : function (jqXHR, settings) {
$('.comment_list').eq(index).append('<dl class="comment_load"><dd>正在加载评论</dd></dl>');
},
success : function (response, status) {
var json_comment = response;
},
});
也许是我对jQuery的parseJSON方法理解错误使用不当,或者是该用其他的方法处理?
后台响应的是JSON格式的字符串而不是JSON对象
如果后台发送的是一个JSON格式的字符串(注意:是字符串,只是采用JSON的语法格式,不是JSON数据),我们在前台应该怎样解析成一个JSON对象呢?
后台代码:
PrintWriter out = response.getWriter();
String username = request.getParameter("user");
String password = request.getParameter("pwd");
String data = "{'username':'"+username+"','password':'"+password+"'}";
System.out.println(data);
out.write(data);
这个data就是一个JSON格式的字符串
我们需要将eval()方法将这个字符串包一下,就可以转成JSON对象了,eval应该是一个JavaScript中的方法:
$.ajax({ //一个Ajax过程
type : "post", //以post方式与后台沟通
url : "Ajax_jQueryServlet", //与此php页面沟通
data : 'username=' + username + '&password=' + password,
success : function(data) {
var json = eval('(' + data + ')');
$('#result').html(
"姓名:" + json.username + "<br/>密码:" + json.password);
}
});
注:eval('('+text+')')将JSON格式的字符串text解析为成具体的类型,如boolean什么的,有时可能需要使用eval('['+text+']') 方括号来包裹JSON字符串,可以解析为数组,也就是Object类型。具体使用看情况吧
jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data的更多相关文章
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...
- js - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data jquery-1.9.1.min.js:3:4315
FF中时不时报这个错, 就近段做项目来看, 一般是我通过 jquery获取form中的参数(或直接获取参数,并通过ajax进行异步请求的时候,如果有错,就抱该错误! 而对应的, 如果在 Google ...
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
在用php返回json数据的时候如果出现这种错误,先检查一下php中是否有使用var_dump()函数 这个函数会在页面输出测试变量的结构,浏览器会将这个当做json数据,所以就报错了....
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 co ...
- json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 column 33 (char 33)
json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 colu ...
- SyntaxError: unexpected character after line continuation character
SyntaxError: unexpected character after line continuation character 待解决问题:在运行.py文件时报错SyntaxError: un ...
- SSH框架-unexpected token: * near line 1, column 8 [select * from tb_chaper where course_id = 2];报错解决方法
SSH项目,访问jsp页面出现报错,控制台显示报错信息: org.springframework.orm.hibernate3.HibernateQueryException: unexpected ...
- Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "</url-pattern>" 终止
1.错误描述 严重: Parse Fatal Error at line 41 column 24: 元素类型 "url-pattern" 必须由匹配的结束标记 "< ...
- 浏览器报错 SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 2 of the JSON data
ajax调用是200,结果返回的不是json字符串(字符串格式不是json应该有的格式),进入了ajax的error回调函数,改为返回json字符串,问题解决了.
随机推荐
- Selenium with Python 009 - WebDriver API
官方API文档:https://seleniumhq.github.io/selenium/docs/api/py/api.html 更多详情,最好的学习方式可以查阅官方API文档或直接阅读源码,本文 ...
- 在一个Activity中启动另一个Activity
一.新建一个空的工程 二.添加一个Activity并命名为BAty 三.在activity_main.xml中添加一个按钮,设置id号为btnStartB <Button android:lay ...
- 【python】命令行解析工具argparse用法
python的命令行参数 之前有用到optget, optparse, 现在这些都被弃用了. import argparse parser = argparse.ArgumentParser() ar ...
- Android 进阶16:IntentService 使用及源码解析
It's time to start living the life you've only imagined. 读完本文你将了解: IntentService 简介 IntentService 源码 ...
- poj2378(dfs,树形dp)
和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...
- Java进阶知识点7:不要只会写synchronized - JDK十大并发编程组件总结
一.背景 提到Java中的并发编程,首先想到的便是使用synchronized代码块,保证代码块在并发环境下有序执行,从而避免冲突.如果涉及多线程间通信,可以再在synchronized代码块中使用w ...
- 关于file.writelines换行符的添加
和file.readlines/readline不同,file.writelines(l)如果l元素没有换行符,writelines是不会自动加入换行符的,需要我们自己添加,就像这样. import ...
- Sublimetext3插件与使用技巧
1. package control 的安装与注意事项 2. 常用插件的安装与注意事项 3. 主题风格设置 4. 常用快捷键 https://packagecontrol.io ...
- IntelliJ IDEA 安装破解详解
https://github.com/tengj/IntelliJ-IDEA-Tutorial IntelliJ IDEA官方中文文档 https://blog.csdn.net/newabcc/ar ...
- 调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/asm/Type
调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/asm/Type 解决方法1: 原因是Spring中的cglib-nodep-2.x.x.jar与Hibernate中 ...