Skip to content
Advertisement

Unable to post data on Jquery ajax

My code to post data to server is like this

  $('#btn').click(function () {

        var myarray = [];
        $("#DocumentList input[type=checkbox]:checked").each(function () {
            myarray.push($(this).attr('uniqueid'));
        });
        alert(myarray);
     
        $.ajax({
            url: "url",
            type: "post",
            dataType: "text",
            data: myarray,
            success: function (response) {
               
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
            }
        });
    });

on alert I am getting the data I want to post to server, But when I inspect the call on Chrome,I can see that data is not getting posted (screenshot added below). What can be the reason for this behavior? enter image description here

Advertisement

Answer

jQuery does not expect you to pass an array of strings to data.

It can’t process that usefully.

Typically you would pass an object of name: value pairs:

data: { something: myarray }

… which will URL encode it with the something[] extended syntax introduced by PHP.

You will need to make sure the encoding you send matches whatever the server expects though.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement