I'm trying to send an array with ajax request to php. Can't understand what I am doing wrong. Can someone please help? This is the code: <script> $(document).ready(function(){ var array = $.makeArray(2); var url = window.location.pathname; var filename = url.match(/.*\/(.*)$/)[1]; var arr = $.toJSON(array); var a = "list=" + arr; $.ajax({ url: filename, context: a, success: function(){alert(a);} }); }); </script> <?php $list = json_decode($_POST['list']); var_dump($list); ?> It says Undefined index: list.
I haven't tried running your code, but try $.ajax({ url: filename, data: "arr=a", success: function(){alert(a); } }); Code (markup): BTW, the default is a GET. If you want POST data, add type: "POST", in the argument list. (Order doesn't matter, but the last item - success, in your case - doesn't end in a comma.)
var str='var1[]='+value1+'&var1[]='+value2; $.ajax({ url:filename.php, data:str, method:'POST', success:function(msg) { alert(msg); } });