1

I am fairly new to JS and AJAX, and for some reason I can not send my dynamically generated and read data via AJAX. How do I properly send an array via AJAX to PHP script?

I have tried following the instructions but I can not get the AJAX to send the data. The first try was a complete bust, the second gets error:

Uncaught TypeError: Illegal invocation

but it seems to originate from the JS-library instead of my code (though the code is most probably the reason for it!) and I do not know what to do with it.

    //first thing I tried
    var i = 1, j = 0, cu = [], cu2 = [];
     while (i <= tilit ) {
        cu[j] = document.getElementById("til_nro_"+i);
        console.log(cu[j].value);

        i++;
    }
    var dynamic_account_value = JSON.stringify(cu);


 jQuery.ajax({
        type: "POST",
         url: 'http:mysite.php',
        dataType: 'json', 
        data: { dynamic_account_count:tilit, db:cu , id:id, result:dynamic_account_value
            }
        });



    //2nd thing I tried
    var i = 1, j = 0, cu = [], cu2 = [];
     while (i <= tilit ) {

        cu[j] = document.getElementById("til_nro_"+i);
        cu2.push(JSON.parse(cu[j].value));

        i++;
    }
    var tilinrot_lisa = JSON.stringify(cu2);


     jQuery.ajax({
        type: "POST",
         url: 'http:mysite.php',
        dataType: 'json', 
        data: { dynamic_account_count:tilit, db:cu , id:id, result:tilinrot_lisa
            }
        });
5
  • 1
    Possible duplicate Commented Sep 10, 2014 at 8:20
  • 1
    Take a look at $.param() api.jquery.com/jquery.param Commented Sep 10, 2014 at 8:20
  • 1
    I think you are trying to stringify DOM nodes, try this instead cu[j] = document.getElementById("til_nro_"+i).value;. Commented Sep 10, 2014 at 8:21
  • http:mysite.php? You probably wanted http://mysite.php Commented Sep 10, 2014 at 9:10
  • Thanks everyone. I got the stringify to work. Do not quite know hy, I re-wrote the 2nd part of the script and now it works. Commented Sep 10, 2014 at 9:44

2 Answers 2

1

First, give them something that makes selection easier, like a common class. Second, use jquery serialize

$.ajax({
  url : "foo",
  data : $(".bar").serialize(),
  success : function(response) {}
})
Sign up to request clarification or add additional context in comments.

2 Comments

Could not get serialize or stringify to work so tried the "untouched" cu as dynaamic:cu however when I read the send data and receive it I only get the last value.
I do not quite know how to serialize it, since I am generating the div id on dynamic content...
0

Try this code snippet, Try to send just one variable then go for another & use content type. u can use //console.log(JSON.stringify(cu)); to view what's inside.

jQuery.ajax({
   type: 'post',
   dataType: 'json',
   data: {your_var:JSON.stringify(cu)},
   contentType: "application/json"

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.