ajax post请求发送json数据报错unexpected token in json at position的问题解决
来源:互联网 发布:网游推荐知乎 编辑:程序博客网 时间:2024/06/05 02:08
报错基本是这个样子的,unexpected token in json at position。
网上查询的解决方案实在是没解决我的问题。
我的错误原因是json数据格式错误。
问题背景是将json格式的数据以post请求的方式发送给nodejs服务器,服务器端解析报错。
发送的测试数据如下:
data = {first_name:"g",last_name:"h"};
上面这个是错误的。正确的如下:
data = {"first_name":"g","last_name":"h"}
后文附服务器端代码和测试代码
【附】
服务器端
var express = require('express');var bodyParser = require('body-parser');var app = express();app.use(bodyParser.urlencoded({extended: false}));app.use(bodyParser.json());app.post('/test', function (req, res) { console.log('req.body', req.body); for(var x in req.body){ console.log(req.body[x]); } res.end("end");});app.listen(8888);测试代码【测试代码我一直是直接敲在浏览器控制台 - -】
data = {"first_name":"g","last_name":"h"};var xhr = new XMLHttpRequest();xhr.onreadystatechange=function(){ if(xhr.readyState==4 && xhr.status==200){ alert(xhr.responseText); }}xhr.open("POST","/test",true); xhr.setRequestHeader("Content-type","application/json");xhr.send(JSON.stringify(data));console.log(xhr.responseText);
阅读全文
0 0
- ajax post请求发送json数据报错unexpected token in json at position的问题解决
- Ajax报错之Unexpected token S in JSON at position
- JSON.parse(str)报错Unexpected token t in JSON at position 1
- JSON.parse() Unexpected token i in JSON at position 2 报错问题
- JSON.parse()报错 Unexpected token i in JSON at position 2
- 模块引用文件路径问题js报错和Unexpected token in JSON at position
- js常见报错之Unexpected token in JSON at position
- js获取富文本编辑器的值之后报错Uncaught SyntaxError: Unexpected token 发 in JSON at position 0
- 关于Uncaught SyntaxError- Unexpected token o in JSON at position 1,chrome持续报错的相关解析
- primefaces 推送服务报Uncaught SyntaxError: Unexpected token < in JSON at position 0
- 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
- 解决方法 Uncaught SyntaxError: Unexpected token < in JSON at position 0
- Uncaught SyntaxError: Unexpected token ] in JSON at position 问题
- 【Angular2】Unexpected token / in JSON at position 35
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- leetcode26 Remove Duplicates from Sorted Array 去重已排序列表
- CC2640
- http错误代码
- 欢迎使用CSDN-markdown编辑器
- 布局相关的知识点
- ajax post请求发送json数据报错unexpected token in json at position的问题解决
- redis 事务
- Linux hung task detect
- 【转载】jxl导出excel(合并单元格)
- VS2013 错误1error C4996: 'wcstok': This function or variable may be unsafe. Consider using wcstok_s i
- 开发框架-Spring-Profile的配置、激活和读取
- linux下wifi编程(基于netlink和nl80211.h)
- 总结
- ReactNative尝鲜