javascript - formData object sent with jQuery ajax to server returns empty array on success -
the title says all.
i collecting data , appending formdata object in order post php file , handle rest there.
my ajax function:
save.addeventlistener("click", function(e){ e.preventdefault(); getallcontents(); console.log(updateobj); updateobj = json.stringify(updateobj); console.log(updateobj); $.ajax({ url: "test.php", type: "post", data: updateobj, success: function(response){ console.log("success: ", response); }, error: function(response){ console.log("error: ", response); } }); });
the updateobj contains appended formdata object. console log of variable returns everything. problem must right in ajax post.
update content of console.log(updateobj) :
[{"row_id":1,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"kunde","ma_name":"aa","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}]
my php file contains print_r
of $_post
<?php print_r($_post); ?>
this success function of ajax call logs :
success: array ( )
update2
this how formdata object gets filled in :
// console.log($test.length); var updateobj = []; function getallcontents(){ var $tabletr = $('tbody tr'); updateobj = []; $tabletr.each(function(index, element){ var $row_id = $(this).data("rowid"); // console.log("id in table: " + $row_id); var status = $(this).find('#status option:selected').val(); // console.log("id "+$row_id+" in der table hat den status: "+status); var ma_name = $(this).find('#ma-name').val(); // console.log(ma_name); var datum = $(this).find('#datum').val(); // console.log(datum); var firmenname1 = $(this).find('#firmenname1').val(); // console.log(firmenname1); var firmenname2 = $(this).find('#firmenname2').val(); // console.log(firmenname2); var limit = $(this).find('#limit').val(); // console.log(limit); var gruppe_kredit = $(this).find('#gruppe_kredit').val(); // console.log(gruppe_kredit); var omv_kdnr = $(this).find('#omv_kdnr').val(); // console.log(omv_kdnr); var sap_kdnr = $(this).find('#sap_kdnr').val(); // console.log(sap_kdnr); var fos = $(this).find('#fos').val(); // console.log(fos); var hga_kdnr = $(this).find('#fos').val(); // console.log(hga_kdnr); var pushobj = { row_id: $row_id, status: status, ma_name: ma_name, datum: datum, fa1: firmenname1, fa2: firmenname2, limit: limit, gruppe_kredit: gruppe_kredit, omv_kdnr: omv_kdnr, sap_kdnr: sap_kdnr, fos: fos, hga_kdnr: hga_kdnr }; updateobj.push(pushobj); // pushobjekt mit inhalt befüllen und das pushobjekt ins updateobjekt einbetten //console.log(updateobj); }); } getallcontents();
the problem trying send array of js objects. in case normal json.stringify(obj)
won't work there no keys on individual objects object wouldn't encoded string on server end won't parsed , wonldn't passed $_post
. 1 solution be
updateobj = [{"row_id":1,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"kunde","ma_name":"aa","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}]; // window.updateobj = console.log(updateobj); obj = {}; $.each(updateobj,function(x,obj){ obj[""+x] = obj;}); // updateobj = json.stringify(updateobj); updateobj = json.stringify(obj); console.log(obj); $.ajax({ url: "//localhost:80/test/", type: "post", data: updateobj, success: function(response){ console.log("success: ", response); }, error: function(response){ console.log("error: ", response); } });
------------------------update----------------------
you can it'll bit tricky decode on server size guess.
updateobj = [{"row_id":1,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"kunde","ma_name":"aa","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}]; console.log(updateobj); obj = {}; obj["updateobj"] = updateobj; updateobj = json.stringify(obj); $.ajax({ url: "//localhost:80/test/", type: "post", data: updateobj, success: function(response){ console.log("success: ", response); }, error: function(response){ console.log("error: ", response); } });
----------update2.0
updateobj = [{"row_id":1,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"testname1","fa2":"testname2","limit":"10.000","gruppe_kredit":"/","omv_kdnr":"8124213","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":3,"status":"anmeldung","ma_name":"aa","datum":"/","fa1":"ame1","fa2":"name2","limit":"12.000","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"},{"row_id":2,"status":"kunde","ma_name":"aa","datum":"/","fa1":"newname1","fa2":"newname2","limit":"15.323","gruppe_kredit":"/","omv_kdnr":"81515616","sap_kdnr":"/","fos":"/","hga_kdnr":"/"}]; // window.updateobj = console.log(updateobj); obj = {}; var obj = updateobj.reduce(function(o, v, i) { o[i] = v; return o; }, {}); updateobj = json.stringify(obj); console.log(obj); $.ajax({ url: "//localhost:80/test/", type: "post", data: updateobj, success: function(response){ console.log("success: ", response); }, error: function(response){ console.log("error: ", response); } });
Comments
Post a Comment