2016-11-15 65 views

回答

0

如果你想使用GET,你可以通過通過URL變量進行編碼,並將它們添加到URL請求,就像這樣:

var url = "/path/to/myservlet"; 
var params = "somevariable=somevalue&anothervariable=anothervalue"; 
var http = new XMLHttpRequest(); 

http.open("GET", url+"?"+params, true); 
http.onreadystatechange = function() 
{ 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
} 
http.send(null); 

如果你有長DATAS通過,使用POST是首選方式,這裏就是這種代碼的一個例子:

var http = new XMLHttpRequest(); 
var url = "/path/to/myservlet"; 
var params = "lorem=ipsum&name=binny"; 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

http.onreadystatechange = function() {//Call a function when the state changes. 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
} 
http.send(params); 

您將能夠使用getParameter(String name)方法在servlet HttpServletRequest閱讀這些DATAS。

希望這會有所幫助。 :-)

+0

感謝烏爾回覆(xmlhttp.open( 「GET」, 「ClientDetails」 + 「?客戶=」 + documnt.getElementById( 「客戶端」)。值+ 「&接觸=」 + documnt.getElementById ( 「聯繫人」)值,假); xmlhttp.send();如果 (xmlhttp.readyState == 4){ 如果(xmlhttp.status == 200){ 警報 (xmlhttp.responseText); document.getElementById(「address」)。value = xmlhttp.responseText; document.getElementById(「city」)。value = xmlhttp.responseText; } }這是我的腳本請檢查一下是對還是錯 – Cinthiyal

1
jQuery.ajax({ 
     url : "<portlet:resourceURL id='URL'/>", 
     data : { 
      "A":value, 
      "B":Value 
     },type : "POST", 
     dataType : "json", 
     success : function(data) { 
     } 
+0

謝謝,我另一個qstn。我怎樣才能從Ajax成功接收servlet的多個響應 – Cinthiyal

+0

你可以在json對象中添加所有響應到servlet中,並將它傳遞給View並解析Json對象 – Khusboo

+0

如果你不介意請給出一個例子,謝謝你 – Cinthiyal

相關問題