2016-08-31 85 views
0

我想在我的頁面上使用ajax,我想通過url設置密鑰值對。Pass = in ajax url

xhttp.open("POST", "/ajax/myjsp.jsp?str=key1=value1|key2=value2" , true); 

論myjsp.jsp它應該給鍵1 =值做的request.getParameter( 「STR」)|鍵2 =值。

+3

使用'encodeURIComponent'確保'='被正確編碼爲'%3D'等 –

回答

0

它工作,因爲它張貼在我的問題。錯誤是它沒有得到ajax文件路徑。剛剛刪除/在一開始,它的工作。

0

function myFunction() { 
 
    var uri = "/ajax/myjsp.jsp?str=key1=value1|key2=value2"; 
 
    var res = encodeURI(uri); 
 
    document.getElementById("demo").innerHTML = res; 
 
}
<p>Click the button to encode a URI.</p> 
 

 
<button onclick="myFunction()">Try it</button> 
 

 
<p id="demo"></p>

+0

我知道OP已經評論說,這個問題是在其他地方,但它的價值請注意,encodeURI不編碼任何在URI中有效的字符。所以如果'str'的​​值包含一個'&',這將被允許,服務器會將該字符視爲'str'的​​結尾。優先考慮如下內容:'var str ='key1 = value1 | key2 = value2; var uri ='/ajax/myjsp.jsp?str='+ encodeURIComponent(str);' –