簡體   English   中英

Ajax不發送數據作為參數

[英]Ajax not sending data as parameters

我試圖通過ajax發送數組,所以我有以下代碼:

這是代碼

 function fncGetChecksToProcess() { var allChecks = []; $('input[type=text]').each(function() { var key = $(this).attr("id").replace("txt_", ""); allChecks[key] = []; }); $('input[type=checkbox]').each(function() { if (this.checked) { var className = $(this).attr('class'); if (className.includes('box_total_')) { var ref = $(this).attr('id').replace("box_total_", ""); var amountDetails = []; var docNs = []; $('.' + ref).each(function() { amountDetails.push(parseFloat($(this).closest('td').next().html().replace("$", "").replace(",", ""))); docNs.push($(this).attr('id').replace("box_", "")); }); allChecks[ref].push({ docN: docNs, amountTot: $("#sub_" + ref).text(), amountDetails: amountDetails, chkNum: $("#txt_" + ref).val() }); } else { var docN = $(this).attr('id').replace("box_", ""); allChecks[className].push({ docN: docN, amountTot: $("#td_" + docN).text(), amountDetails: "", chkNum: $("#txt_" + className).val() }); } } }); return allChecks; } $(document).ready(function() { $("#btn").click(function() { var checks = fncGetChecksToProcess(); console.log(checks); $.ajax({ cache: false, type: 'POST', data: { allChecks: checks }, url: '/process', beforeSend: function() { console.log("Processing your checks please wait..."); }, success: function(response) { console.log(response); }, error: function() { console.log("Error"); } }); }); }); 
 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script src="app.js"></script> </head> <body> <table id="table" class="tablesorter" width="100%" cellspacing="0"> <thead> <tr> <th>H1</th> <th>H2</th> <th>H3</th> <th>H4</th> <th>H5</th> <th></th> <th>H6</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> <td>11002WR</td> <td>201100</td> <td>A</td> <td class="center"><input class="11002WR" id="box_201100" type="checkbox" /></td> <td id="td_201100">$320.00</td> </tr> <tr> <td colspan="3"></td> <td>Check. No</td> <td><input id="txt_11002WR" type="text"></td> <td><input id="box_total_11002WR" class="box_total_201100" type="checkbox" /></td> <td id="sub_11002WR">$12.00</td> </tbody> </table> <input id="btn" type="button" value="Test" /> </body> </html> 

請選中兩個復選框和輸入,然后按測試。

控制台會打印出生成的數組,但Ajax不會發送它。

為什么我的ajax調用不發送任何參數? 我沒有在Chrome控制台中看到它們。

謝謝。

由於您的鍵是字符串,而不是數字,因此您應該使用一個對象,而不是數組。 更改

var allChecks = [];

var allChecks = {};

當使用$.ajax發送數組時,它僅發送帶有數字索引的元素,而不發送命名屬性。

您需要將一個數組發送到ajax,現在您要在array內發送一個array數組...如果您像表單那樣發布到服務器,則該發布看起來像:

allchecks[] = x
allchecks[] = y ...

https://plnkr.co/edit/SnZmKgRLODYb8p6kPryn?p=preview

   $.ajax({

          cache: false,
          type: 'POST',
          data: {
            allChecks: checks["11002WR"][0]
          },

這是您的快速解決方案。 但這不是一個好習慣

而不是allchecks = []。 嘗試使用鍵或id而不是像ref這樣的字符串作為數組的鍵來構建對象

對象如下所示:

 var allChecks = {};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM