How to send an array via url in javascript / jQuery

I am trying to send a javascript array via url.But this fails

function viewReport(mode,someid){ if(mode==0){ var para= new Array(); para['para1'] = 'para1'||0; para['para2']= 'para2' || 0; console.log(para); window.open('somePDFView/'+para,'_blank'); }else{ var para=[]; var paraelements={ para1:'anotherpara1'||0, para2:'anotherpara2'||0 }; para[0]=paraelements; window.open('somePDFView/'+para,'_blank'); } } 

In if part(mode=0) para array no longer sends, and on the other part ( mode=1 ), para is sent as follows:

 somePDFView/[object Object] 

What the error shows:

The URI you submitted has illegal characters

How can we send an array via url.I cannot use Ajax (because its a popup) or a session or store in a temporary table. Just like we can get this value in the controller.

Edit:

I miss the important thing that I use codeigniter. Then I think that this prohibits special characters such as - &, =, [,], etc. So if any other methods are available to send data as an array? ..

+4
source share
4 answers

To send an array of parameters via a URL from Javascript to PHP / Codeigniter, you need to follow these steps:

  • Convert the params array to a JSON string and send it as one request parameter.
    para = JSON.stringify(para);
    window.open('somePDFView?params='+para,'_blank');

  • Decode a query string parameter (e.g. a JSON string) on ​​your controller
    $params = json_decode($_GET['params']);

Now you can use all the parameters on your controller by contacting via $ params.

Your code will be:

  function viewReport (mode, someid) {
     if (mode == 0) {
         var para = new Array ();
         para ['para1'] = 'para1' || 0;
         para ['para2'] = 'para2' ||  0;
         console.log (para);
         para = JSON.stringify (para);
         window.open ('somePDFView? params =' + para, '_ blank'); 
     } else {
         var para = [];
         var paraelements = {
           para1: 'anotherpara1' || 0,
           para2: 'anotherpara2' || 0
         };
         para [0] = paraelements;
         para = JSON.stringify (para);
         window.open ('somePDFView? params =' + para, '_ blank'); 
     }
 }

Try this and let me know if you are still experiencing this issue.

+8
source

You can also use Json . Use JSON.stringify() .

Note: Do not send long data to the URL. There is a limit to sending data to a URL, and as a result, it will be damaged if it exceeds the limit. For big data, use the POST method.

 function viewReport(mode,someid){ var json; if(mode==0){ var para= new Array(); var paraelements={ para1:'para1'||0, para2:'para2'||0 }; para[0]=paraelements; json = JSON.stringify(para); window.open('somePDFView/'+json,'_blank'); }else{ var para=[]; var paraelements={ para1:'anotherpara1'||0, para2:'anotherpara2'||0 }; para[0]=paraelements; json = JSON.stringify(para); window.open('somePDFView/'+json,'_blank'); } } 

If you are using php, use json_decode() to convert json to a PHP variable. Also refer, http://php.net/manual/en/function.json-decode.php

+6
source

You can try to pass them as individual parameters:

 var para1 = '1'; var para2 = '2'; var para= new Array(); para.push('para[]=' + para1 || 0); para.push('para[]=' + para2 || 0); var url = para.join('&'); alert(url) 

Returns para[]=1¶[]=2 . Please note that jQuery is not required for this solution.

+3
source

Use jQuery.param () and change the structure of your data:

 var para = { para1:'anotherpara1'||0, para2:'anotherpara2'||0 }; window.open('somePDFView/'+jQuery.param(para), '_blank'); 

Demo jsFiddle

+2
source

Source: https://habr.com/ru/post/1498711/


All Articles