[英]Array via POST (Ajax)
Okay, this seems to be the most straight forward thing, but I really have no idea why it's doing this nor find anyone else with this problem. 好的,这似乎是最直接的方法,但是我真的不知道为什么要这样做,也找不到其他人遇到这个问题。
Here's my issue, I'm sending a POST request like so; 这是我的问题,我正在像这样发送POST请求;
$.ajax({
type: "POST",
url: '/user/sell',
data: data,
success: function(data) {
console.log('Call was successful');
}
});
In the data object is an array called items
. 在数据对象中是一个称为items
的数组。 When I log the data object it's fine, like it should be, however when I log the data object in my express function the items
array changes to items[]
for no reason.. 当我记录数据对象时,就像应该的那样,它很好,但是当我在Express函数中记录数据对象时, items
数组无缘无故地变为items[]
。
NodeJS 的NodeJS
'items[]': '15716345'
JS (Browser) JS(浏览器)
items: [15716345]
Any idea what's happening here? 知道这里发生了什么吗?
Below is the entire version of the code. 下面是代码的完整版本。 Entire block (frontend) // Validate address if($('.block.payment .wrapper input:eq(0)').val() !== $('.block.payment .wrapper input:eq(1)').val()){ return error('Fields do not match'); 整个块(前端) //验证地址if($('。block.payment .wrapper输入:eq(0)')。val()!== $('。block.payment .wrapper输入:eq(1) ').val()){返回错误(“字段不匹配”); } }
// Get known data
var type = $('.body.inventory .methods .method.selected').data('type'),
items = [];
var data = {
type,
address: $('.block.payment .wrapper input:eq(0)').val()
}
if(type === 'steam'){
var app = $('.body.inventory .sub-methods .method.selected').data('app');
data['app'] = app;
$('.body.inventory .item[data-app="'+app+'"].selected').each(function(){
items.push($(this).data('id'));
});
}else{
$('.body.inventory .item[data-type="'+type+'"].selected').each(function(){
items.push($(this).data('id'));
});
}
data['items'] = items;
// Execute route or smt
$.ajax({
type: "POST",
url: '/user/sell',
data: data,
success: function(data) {
console.log('Call was successful');
}
});
Backend 后端
router.post('/sell', function(req, res, next) {
try {
console.log(req.body);
res.send({
success: 1
});
} catch(e) {
if(e) console.log(e);
res.send({
success: 0,
error: e
});
}
});
Set JSON
body parser middleware for requests to your expressJS application. 设置JSON
主体解析器中间件 ,以获取对您的expressJS应用程序的请求。
const bodyParser = require('body-parser');
app.use(bodyParser.json())
And in the AJAX request, make the contentType
to be application/json
and not the default of application/x-www-form-urlencoded; charset=UTF-8'
然后在AJAX请求中,将contentType
设置为application/json
而不是application/x-www-form-urlencoded; charset=UTF-8'
的默认值application/x-www-form-urlencoded; charset=UTF-8'
application/x-www-form-urlencoded; charset=UTF-8'
. application/x-www-form-urlencoded; charset=UTF-8'
。
$.ajax({
contentType: 'application/json',
type: "POST",
url: '/user/sell',
data: data,
success: function(data) {
console.log('Call was successful');
}
});
object[] Obj = new object[1];
Obj [0] = "value1"
Obj [1] = "Value2"
Obj [3] = {"CollectionValue1, CollectionValue2"}
$.ajax({
url: '../Controller/MethodName',
type: 'post',
datatype: 'json',
async: false,
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ ControllerParameterName: Obj }), <!-- Obj is your Array -->
success: function (data) {
alert(data.Response);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.