2014-12-04 74 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