2017-12-27 4 views
0

Ich versuche, eine Reihe über Ajax zu senden, so habe ich den folgenden Code:Ajax keine Daten sendet als Parameter

Hier ist der Code

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>

ist

Bitte überprüfen Sie die zwei Kontrollkästchen und die Eingabe und drücken Sie auf den Test.

Die Konsole gibt das generierte Array aus, aber Ajax sendet es nicht.

Warum sendet mein Ajax-Anruf keine Parameter? Ich sehe sie nicht in der Chromkonsole.

Danke.

+1

FYI können Sie '$ (" input [type = Checkbox] : checked ")' so brauchst du nicht 'if (this.checked)' – Barmar

+0

danke für den Hinweis. Irgendwelche Ideen, warum ich die Ajax-Ausgabe habe –

+0

Nein, habe diesen Teil noch nicht herausgefunden. – Barmar

Antwort

2

Da Ihre Schlüssel Zeichenfolgen sind, keine Zahlen, sollten Sie ein Objekt und kein Array verwenden. Ändern

var allChecks = []; 

zu

var allChecks = {}; 

Wenn Sie einen Array mit $.ajax senden, ist es nur die Elemente mit numerischer Indizes sendet, nicht benannte Eigenschaften.

+0

Scheint, dass JS-Arrays String-Indizes zugewiesen werden können, solange sie numerisch sind. Wie oben erwähnt, erzeugt es jedoch ein spärliches Array. – Phil

+0

Könnte das der Grund für das Ajax-Problem sein? –

+1

@Phil Aber seine Indizes sind nicht numerisch, sie sind Dinge wie '11002WR' – Barmar

0

Sie benötigen ein Array Ajax zu senden, haben Sie jetzt eine Reihe von Array innerhalb Array senden ... , wenn Sie an den Server wie Form Post Die Post Willen wie folgt aussieht:

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

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

$.ajax({ 

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

Dies ist eine schnelle Lösung für Sie. aber es ist keine gute Praxis

anstelle von allchecks = []. versuchen, Objekte mit Schlüsseln oder IDs zu bauen und nicht die Zeichenfolge wie der ref als Schlüssel des Arrays

Objekte wie das ist folgende:

var allChecks = {};