Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?..

share|improve this question
1  
You might want to use serialize or param – Brewal Aug 24 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. – putvande Aug 24 at 10:01
serialize? k I will study that one and try it... – manuthalasseril Aug 24 at 10:02
Don't use arrays with non-numeric keys! – Felix Kling Aug 24 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?) – Christian Aug 24 at 10:50

3 Answers

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

share|improve this answer
Its also showing the error --The URI you submitted has disallowed characters. – manuthalasseril Aug 24 at 10:19
Use console.log(json) and check what data is being return before sending through url. – raduns Aug 24 at 10:22
the data is correctly assigned and json contains all that values but it can't send – manuthalasseril Aug 24 at 10:28
Are you sending the data by using query string? – raduns Aug 24 at 10:32
yes.I am using query string. – manuthalasseril Aug 24 at 10:34
show 5 more comments

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

share|improve this answer
In alert its working . but on window.open its not working .why??? – manuthalasseril Aug 24 at 10:19
You can't do this. See this tread or this one. – Brewal Aug 25 at 14:04

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.

share|improve this answer
sorry its not working... – manuthalasseril Aug 24 at 10:10
Post edited. The code is now tested and working. – Milan Milanov Aug 24 at 10:19
Now i can see the parameters on url--salesexePDFView/parat[] = 1&parat[] = 2 but it shows The URI you submitted has disallowed characters. – manuthalasseril Aug 24 at 10:24
I removed the spaces between parat[] and the = sign. It should work now. – Milan Milanov Aug 24 at 10:27
sorry .its not working :( – manuthalasseril Aug 24 at 10:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.