5

I am trying to send a javascript array via the url.But it 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'); 
    }
}

On if part(mode=0), the para array is not sending any more and on else part (mode=1) the para is sends like this:

somePDFView/[object Object]

Which shows the error:

The URI you submitted has disallowed characters

How can we send an array via url.I can't use Ajax (because its a popup window) or session or storing in a temporary table.Also how can we retrieve this value in the controller.

Edit:

I miss an important thing that I am using codeigniter. Then I think it disallows special characters like - &,=,[,],etc..So if any other methods available for sending data as an array?..

5
  • 2
    You might want to use serialize or param Commented Aug 24, 2013 at 9:58
  • You could encode it. But a URL has a maximum length to send variables I think, so don't make it too long. Commented Aug 24, 2013 at 10:01
  • serialize? k I will study that one and try it... Commented Aug 24, 2013 at 10:02
  • Don't use arrays with non-numeric keys! Commented Aug 24, 2013 at 10:15
  • I think you should first study why string + array produces strange results... (hint: what happens when you combine a car with an apple?) Commented Aug 24, 2013 at 10:50

4 Answers 4

9

To send an array of params via URL from Javascript to PHP/Codeigniter, you need to follow 2 steps:

  1. Convert your params array to JSON String and send that as a single query param.
    para = JSON.stringify(para);
    window.open('somePDFView?params='+para,'_blank');

  2. Decode the query string param (i.e JSON string) at your controller
    $params = json_decode($_GET['params']);

Now you can use all the params at your controller by accessing through $params.

Your code block becomes:

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 out and let me know if you still faces this issue.

Sign up to request clarification or add additional context in comments.

Comments

6

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

Note: Don't sent long data over a URL. There is a limit for sending data via url and it will end up in a corrupted data if exceeded the limit. For large data, use 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 into PHP variable.Also refer, http://php.net/manual/en/function.json-decode.php

9 Comments

Its also showing the error --The URI you submitted has disallowed characters.
Use console.log(json) and check what data is being return before sending through url.
the data is correctly assigned and json contains all that values but it can't send
Are you sending the data by using query string?
Sorry I miss a important thing.I am using codeigniter, so i think it does not allows special characters like &,=,[,].Is it right?
|
3

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&para[]=2. Note that this solution does not require jQuery.

2 Comments

Now i can see the parameters on url--salesexePDFView/parat[] = 1&parat[] = 2 but it shows The URI you submitted has disallowed characters.
I removed the spaces between parat[] and the = sign. It should work now.
2

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

var para = {
    para1:'anotherpara1'||0,
    para2:'anotherpara2'||0
};

window.open('somePDFView/'+jQuery.param(para), '_blank'); 

The Demo jsFiddle

1 Comment

You can't do this. See this tread or this one.

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.