3

I want to pass an array as url GET parameter and redirect to new page. I don't know how to pass an array with window.location.href. Is there any way in Ajax to redirect to new page with array as parameter(with or without window.location.href) or is there any other function for redirecting to other page in which I can pass an array as parameter from the front-end.

Utkarsh Garg
  • 125
  • 1
  • 2
  • 12

2 Answers2

7

with jQuery $.param function you can convert array to http query

var data = {myArr: [1,2,3,4,5]};
console.log("index.php?" + $.param(data));    
// index.php?myArr%5B%5D=1&myArr%5B%5D=2&myArr%5B%5D=3&myArr%5B%5D=4&myArr%5B%5D=5
Peter
  • 16,453
  • 8
  • 51
  • 77
2

Try this

 info[0] = 'hi';
 info[1] = 'hello';
 info = JSON.stringify(info);

           $.ajax({
                url: "index.php?info="+info,
                data: data_to_send,
                success: function(msg){
                    $('.answer').html(msg);
                }
            });

In backend code just json_decode the info and use.

K.B
  • 885
  • 5
  • 10