Javascript jQuery赢得';t将包含数组的JSON作为其属性之一正确发布

Javascript jQuery赢得';t将包含数组的JSON作为其属性之一正确发布,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我试图使用jQuery将一个JSON对象发送到API端点,该对象的属性之一是数组。我这样定义它: let bidding = { "name": $("#name").val(), "applicant": $("#applicant").val(), "start_date": $("#start_date").val(), "end_date": $("#end_date").val(), "products": [] } $("#products").children

我试图使用jQuery将一个JSON对象发送到API端点,该对象的属性之一是数组。我这样定义它:

let bidding = {
  "name": $("#name").val(),
  "applicant": $("#applicant").val(),
  "start_date": $("#start_date").val(),
  "end_date": $("#end_date").val(),
  "products": []
}

$("#products").children(".dropdown").each(function(key, value) {
  bidding.products.push(
    {
    "product_name": $(value).children(".btn").text(),
    "quantity": $(value).children("input").val()
    }
  );
});
{
  name: 'Material de Construção',
  applicant: 'Prefeitura',
  start_date: '26/09/2017',
  end_date: '01/10/2017',
  'products[0][product_name]': 'Cimento (5kg)',
  'products[0][quantity]': '200',
  'products[1][product_name]': 'Tijolo',
  'products[1][quantity]': '100',
  'products[2][product_name]': 'Caneta',
  'products[2][quantity]': '5'
}
当我执行
console.log(JSON.stringfy(bidding))
时,它会按预期进行解析,例如:

{
  "name":"Material de Construção",
  "applicant":"Prefeitura",
  "start_date":"26/09/2017",
  "end_date":"01/10/2017",
  "products":[
    {"product_name":"Cimento (5kg)","quantity":"200"},
    {"product_name":"Tijolo","quantity":"100"},
    {"product_name":"Caneta","quantity":"5"}
  ]
}
但是当我使用
$.POST(“/api”,bidding)发布它时我的API接收它的方式如下:

let bidding = {
  "name": $("#name").val(),
  "applicant": $("#applicant").val(),
  "start_date": $("#start_date").val(),
  "end_date": $("#end_date").val(),
  "products": []
}

$("#products").children(".dropdown").each(function(key, value) {
  bidding.products.push(
    {
    "product_name": $(value).children(".btn").text(),
    "quantity": $(value).children("input").val()
    }
  );
});
{
  name: 'Material de Construção',
  applicant: 'Prefeitura',
  start_date: '26/09/2017',
  end_date: '01/10/2017',
  'products[0][product_name]': 'Cimento (5kg)',
  'products[0][quantity]': '200',
  'products[1][product_name]': 'Tijolo',
  'products[1][quantity]': '100',
  'products[2][product_name]': 'Caneta',
  'products[2][quantity]': '5'
}

如何使jQuery停止为数组中的每个条目创建新属性,而是将整个数组作为单个属性发送?

您需要设置为false:

:默认情况下,作为对象传递到数据选项的数据(从技术上讲,不是字符串)将被处理并转换为查询字符串,符合默认内容类型“application/x-www-form-urlencoded”。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false

因此,你的职位将是:

$.ajax({
    type: "POST",
    url: "/api",
    data: JSON.stringify(bidding),
    processData: false,
    contentType: "application/json",
    dataType:"json",
    success: function () {
    }
});
在服务器端解码json。如果您使用的是php,请使用


使用json保存数据结构总是一个好主意

好的,所以我继续用“$.ajax({type:“post”,url:/api),data:json.stringify(bidding),processData:false,contentType:“application/json”,dataType:“json”,success:function(){}};”替换了我的“$.post({api”,bidding);”这做得很好。我确实尝试过,但结果太麻烦了,因为我将Express/Node.js与BodyParser一起使用,而BodyParser解析字符串时出错了。