2014-12-04 104 views
1

我坚持在Java脚本侧的问题编码URL参数

1)

var text='{"type": "comTocomp","data":[{ "1" : "Key Parameters" , "2" : "Steel - Sponge/ Pig Iron"}, 
      { "1" : "No of Companies" , "2" : "6,6 % " }]}'; 

这是我的VAR的文字我想给它的服务器端生成Excel表格 ,也是浏览器应该弹出提示,以将文件保存为此我做

2)

 var url="/XXX/YYYY/genrateSheet";  // URL OF CONTROLLER 
     var encode=url+ "?data="+text;   // URL + PARAMETER+ TEXT CONTENT 
     var resUri=encodeURIComponent(encode); // TRIED TO ENCODE BUT DOESN'T WORK 
     **window.location.replace(resUri);**  // CALLING TO CONTROLLER TO PROMPT POPUP 

问题如var text如果它包含一些特殊字符如%。浏览器显示

The character encoding of the plain text document was not declared 

但没有特殊字符,然后工作正常,我。

我有谷歌很多,但要到网址与使用window.location.replace(resUri)

任何帮助,将帮助我很多编码。

由于提前

+0

此无关的Java。 – fge 2014-12-04 14:06:05

+0

@fge右叶错误.... :) – Anurag 2014-12-04 14:11:44

回答

2

你需要编码的查询字符串的值不是查询字符串本身:

var url="/XXX/YYYY/genrateSheet";  // URL OF CONTROLLER 
var resUri = url + "?data=" + encodeURIComponent(text); // querystring 
window.location.replace(resUri); 

话说,text是相当长的,所以你应该考虑post是荷兰国际集团新页面,而不是将其传递到URL中。一个办法做到这一点使用jQuery是创建和提交包含所需数据的隐藏的表单:

$('button').on('click', function() { 

    var text='{"type": "comTocomp","data":[{ "1" : "Key Parameters" , "2" : "Steel - Sponge/ Pig Iron"}, { "1" : "No of Companies" , "2" : "6,6 % " }]}'; 

    // create a hidden form  
    var theForm = $('<form action="/XXX/YYYY/genrateSheet" method="post"/>').hide(); 

    // create a textarea named data and set your json inside it 
    var theTextarea = $('<textarea name="data"/>').val(text); // data in your case 

    // add the textarea to the form 
    theForm.append(theTextarea); 

    // submit the form 
    theForm.submit(); 
}); 

Working jsfiddle

+0

感谢您的回复会现在就来试试.. – Anurag 2014-12-04 14:12:43

+0

我怎么能实现乌拉圭回合第二点.. – Anurag 2014-12-04 14:20:37

+0

非常感谢它为我工作 – Anurag 2014-12-04 14:23:30