Javascript 使用主体解析器bodyParser.json()时为空json
我试图通过HTML页面中的脚本发送一个长json作为post请求,比如:(数据来自一个文本框,它是一个正确的json数组) 我正在使用node express应用程序中的主体解析器读取主体中的json,例如:Javascript 使用主体解析器bodyParser.json()时为空json,javascript,node.js,json,express,body-parser,Javascript,Node.js,Json,Express,Body Parser,我试图通过HTML页面中的脚本发送一个长json作为post请求,比如:(数据来自一个文本框,它是一个正确的json数组) 我正在使用node express应用程序中的主体解析器读取主体中的json,例如: app.post('/api/updateOrgs', jsonParser, (req, res)=> { try { console.log(req.body); // send response res.send('Suc
app.post('/api/updateOrgs', jsonParser, (req, res)=> {
try {
console.log(req.body);
// send response
res.send('Successfully updated');
} catch (e) {
res.send(e);
}
});
问题是我的express应用程序打印了一个空对象{}
。那是因为我发布的json文件非常大吗?它在一个数组中有64个对象
或者,问题来自使用express应用程序,该应用程序将主体解析器模块用作
app.post('/api/updateOrgs',jsonParser,(req,res)=>{
?对象bodyParser
公开各种工厂以创建中间件。当内容类型请求标头与类型选项或空对象({})匹配时,所有中间件都将使用解析的正文填充req.body属性如果没有要分析的正文,则说明内容类型不匹配,或者发生错误
bodyParser.json([options])
返回仅解析json并仅查看内容类型标头与类型选项匹配的请求的中间件。此解析器接受正文的任何Unicode编码,并支持自动扩展gzip和deflate编码
包含解析数据的新主体对象填充在中间件之后的请求对象上(即req.body)
更改解析器的接受类型
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))
bodyParser
对象公开了各种工厂来创建中间件。当Content-Type请求头与Type选项或空对象({})匹配时,所有中间件都将使用解析的主体填充req.body属性如果没有要分析的正文,则说明内容类型不匹配,或者发生错误
bodyParser.json([options])
返回仅解析json并仅查看内容类型标头与类型选项匹配的请求的中间件。此解析器接受正文的任何Unicode编码,并支持自动扩展gzip和deflate编码
包含解析数据的新主体对象填充在中间件之后的请求对象上(即req.body)
更改解析器的接受类型
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))
试试看:
dataType: 'json',
contentType: 'application/json',
data : JSON.stringify({json:JSON.parse(data)}),
所有代码:
<script>
/* UPDATE ORGANIZATION LIST*/
function updateOrgs () {
var data = $('#showOrgs').val();
$.ajax({
url : "http://localhost:8000/api/updateOrgs",
type: "POST", // data type (can be get, post, put, delete)
dataType: 'json',
contentType: 'application/json',
data : JSON.stringify({json:JSON.parse(data)}), // data in json format
async : false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
success: function(response, textStatus, jqXHR) {
alert(response);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown)
}
});
}
</script>
/*更新组织列表*/
函数updateOrgs(){
var data=$('#showOrgs').val();
$.ajax({
url:“http://localhost:8000/api/updateOrgs",
类型:“POST”,//数据类型(可以是get、POST、put、delete)
数据类型:“json”,
contentType:'应用程序/json',
data:JSON.stringify({JSON:JSON.parse(data)}),//JSON格式的数据
async:false,//启用或禁用async(可选,但如果以后需要填充数据,建议为false)
成功:函数(响应、文本状态、jqXHR){
警报(响应);
},
错误:函数(jqXHR、textStatus、errorshown){
警报(错误抛出)
}
});
}
试试看:
dataType: 'json',
contentType: 'application/json',
data : JSON.stringify({json:JSON.parse(data)}),
所有代码:
<script>
/* UPDATE ORGANIZATION LIST*/
function updateOrgs () {
var data = $('#showOrgs').val();
$.ajax({
url : "http://localhost:8000/api/updateOrgs",
type: "POST", // data type (can be get, post, put, delete)
dataType: 'json',
contentType: 'application/json',
data : JSON.stringify({json:JSON.parse(data)}), // data in json format
async : false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
success: function(response, textStatus, jqXHR) {
alert(response);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown)
}
});
}
</script>
/*更新组织列表*/
函数updateOrgs(){
var data=$('#showOrgs').val();
$.ajax({
url:“http://localhost:8000/api/updateOrgs",
类型:“POST”,//数据类型(可以是get、POST、put、delete)
数据类型:“json”,
contentType:'应用程序/json',
data:JSON.stringify({JSON:JSON.parse(data)}),//JSON格式的数据
async:false,//启用或禁用async(可选,但如果以后需要填充数据,建议为false)
成功:函数(响应、文本状态、jqXHR){
警报(响应);
},
错误:函数(jqXHR、textStatus、errorshown){
警报(错误抛出)
}
});
}
您可能缺少内容类型:application/json header是否适用于small json?您可能缺少内容类型:application/json header是否适用于small json?