weixin_33713350 2018-12-12 06:52 采纳率: 0%
浏览 67

使用两个数组的Ajax请求

I have two arrays. Both are getting from two different $.each() functions.

var firstArray = ['item1', 'item2', 'item3', 'item4']; // **Dynamic Values
var counter = 0;
$.each(function(i, v){

  //some code....
  secondArray[counter]  = $(this).val();

  //secondArray have these values ['value1', 'value2', 'value3', 'value4']

  counter++;
  if(counter >= 4){
    $.ajax({
      type: 'POST',
      url: customURL,
      data: ?????
    });
  } 
});

Actually i want the result like the 'item1' will get the 'value1', 'item2' to 'value2' and so on. In past when i used static values like -

$.ajax({
  type: 'POST',
  url: customURL,
  data: {
    'item1' : value[0],
    'item2' : value[1],
    'item2' : value[2],
    'item3' : value[3],
   }

});

i got the results using these static values but how could i use dynamic values instead of 'item1', 'item2' and so on. Remember the 'firstArray' values are dynamic means they can be changed. I am using only jQuery and HTML.

  • 写回答

2条回答 默认 最新

  • weixin_33725515 2018-12-12 07:03
    关注

    you only can send one data set with one request. so perhaps it's an option for you to merge your set before

    Just sample code

    let firstArray = [1, 2, 3, 4];
    let secondArray = ['a', 'b', 'c'];
    
    let dataSet = {
      first: firstarray, 
      second: secondArray
    };
    
    $.ajax({
      url: customUrl,
      method: 'POST',
      accepts: 'application/json',
      dataType: 'json',
      data: dataSet
    })
      .done((res, status, xhr) => {
        // whatever you need to do
      })
      .fail((xhr, status, state) => {
        // whatever you need to do
      });
    

    after that you have to handle the two arrays at backend

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。