亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 POST 數組(Ajax)

通過 POST 數組(Ajax)

HUWWW 2021-06-29 13:01:45
好吧,這似乎是最直接的事情,但我真的不知道為什么要這樣做,也不知道其他人有這個問題。這是我的問題,我像這樣發送 POST 請求;  $.ajax({      type: "POST",      url: '/user/sell',      data: data,      success: function(data) {        console.log('Call was successful');      }    });在數據對象中有一個名為 的數組items。當我記錄數據對象時它很好,就像它應該的那樣,但是當我在我的快速函數中記錄數據對象時,items數組items[]無緣無故地更改為..節點'items[]': '15716345'JS(瀏覽器)items: [15716345]知道這里發生了什么嗎?下面是代碼的完整版本。 整個區塊(前端) // 驗證地址 if($('.block.payment .wrapper input:eq(0)').val() !== $('.block.payment .wrapper input:eq(1) ').val()){ return error('字段不匹配'); }// Get known datavar 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');  }});后端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    });  }});
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

為您的 expressJS 應用程序的請求設置JSON正文解析器中間件。


const bodyParser = require('body-parser');


app.use(bodyParser.json())

并且在 AJAX 請求中,使contentType成為application/json而不是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');

  }

});


查看完整回答
反對 回復 2021-07-01
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

假設這是您要發布的數組列表。

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);

  }

});


查看完整回答
反對 回復 2021-07-01
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號