Search code examples
jsonajaxpostserializationserializearray

data.push is not working with ajax


I am trying to send an extra data with ajax. I used (form).serializeArray() and added an extra data. when I am trying to send it via ajax, the alert showed me that the extra data wasn't added to the the serialized array. This is my code:

 $('#addrows').on('click',function(e){


        var imgsrc=  document.getElementById("imgz").src;
        alert(imgsrc);



    e.preventDefault();

    var data_save = $('form').serializeArray();

      data_save.push({ name: "imgname", value: imgsrc});


    $.ajax({
    url:"add.php",
    cache:false,
    method:"POST",
    data:data_save,

    success: function(data_save){

        var obj = JSON.parse(data_save);
        console.log(obj);
t.row.add( obj ).draw( false );
alert(obj);


    }

});
        }); 
        });

Solution

  • I have tested the above example and everything seems fine over there. Also I dont see alert after:

    var data_save = $('form').serializeArray();
    
    data_save.push({ name: "imgname", value: imgsrc});
    

    So try adding:

    console.log(data_save);
    

    and check the response or send me the screenshot of data that get passed in developer console.