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 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥100 华为ens111111111111111111111111111111111111111111
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题