2016-11-15 12 views
0

私はjspajaxを初めて使っています。javascriptでajaxを使用してjspからサーブレットに複数の変数を渡す方法

xmlhttp.open("GET",servlet,false);をからjspに変更して複数の変数をサーブレットに渡す方法。

名前と電話番号のような2つの選択ボックスがあります。サーブレットに選択した値を送信する必要があります。サーブレットでは、ajaxを使用してjspに複数の詳細のような都市を渡します。

答えて

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); 

あなたが合格に長い件のデータを持っている場合は、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)メソッドを使用してサーブレットHttpServletRequestでこれらの件のデータを読み取ることができます。

これが役に立ちます。 :-)ウル返事(xmlhttp.open( "GET"、 "ClientDetails" + "?クライアント=" + documnt.getElementById( "クライアント")のための

+0

感謝。値+ "&接触=" + documnt.getElementById 。)、偽( "接触")値; xmlhttp.send(); IF(xmlhttp.readyState == 4){ IF(xmlhttp.statusの== 200){ アラート(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

ありがとう、私は別のhv qstn。どのように私はサーブレットからAJAXの成功で複数の応答を受け取ることができます – Cinthiyal

+0

サーブレットのjsonオブジェクトのすべての応答をサーブレットに追加し、それをViewに渡してJsonオブジェクトを解析します – Khusboo

+0

あなたはそれについて一つの例を挙げてください。 – Cinthiyal

関連する問題