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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同