2014-01-05 30 views
1

我使用jquery通過servlet從數據庫中獲取值。我的腳本中的回調函數爲我提供了來自database.h的原始信息。我可以在jsp中追加這些值來選擇選項。如何在jquery中使用回調數據獲取對象?

這裏是我的Retrive_country servlet代碼:

String sql1 = "SELECT * FROM state WHERE country_ref="+countryref+"      
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1); 
ResultSet j = pst1.executeQuery(); 

while (j.next()) { 
    state_id = j.getString(1); 
    state = j.getString(2); 
    country_ref = j.getString(3); 
    location.setState(state); 
    location.setState_id(state_id); 
    location.setcountry_ref(country_ref); 
    pw.println(state_id); 
    pw.println(state); 
    pw.println(country_ref); 
} 

這裏是我的腳本:

<script> 
$(document).ready(function(){ 

    $("#country_id").change(function() { 
     var xyz = $("option:selected").val(); 
     alert(xyz) 
     $.get("../Retrive_country?countryREF="+xyz", 
     {countryREF : xyz }, 
      function(data){ 
       console.log(data); 
       alert("Data: " + data); 
      }); 

    }); 
}); 
</script> 

這裏是我的jsp:

<div class="span2 clear"> 
<select name="country_id" id="country_id"> 
<option>-select-</option> 


<option id="blabbb">1</option> 
<option id="blabbb">2</option> 
<option id="blabbb">3</option> 

</select></div> 

<div class="span2 clear"> 
<select name="state_ref" id="state_ref"> 
<option ></option> 
</select></div> 

這裏是我的控制檯輸出:

enter image description here

所有的字符串是狀態值和整數STATEID。

我想讓它們在jsp中分開使用。

+0

您是否有一些示例後端響應? –

+0

你可以將你的回覆作爲json發送嗎? –

回答

1

您應該更好地使用後端JSON編碼器。但本手冊編碼應該工作,太:

後端代碼:

pw.println("["); 
while (j.next()) { 
    state_id = j.getString(1); 
    state = j.getString(2); 
    country_ref = j.getString(3); 
    pw.println("{stateId: " + state_id + ", stateName: \"" + state +"\"},"); 
} 
pw.println("]"); 

(我假設你是STATE_ID整數)

客戶端代碼:

$("#country_id").change(function() { 
    var xyz = $("option:selected").val(); 
    $.get("../Retrive_country?stateadd_1=none", {countryREF : xyz }, function(data){ 
     var states = eval(data); 
     $('#state_ref').empty(); 
     $.each(states, function(index, state){ 
      $("<option></option>") 
       .attr("value", state.stateId).text(state.stateName) 
       .appendTo('#state_ref'); 
     });   
    }, 'text'); 
}); 

歡呼聲中,從玻利維亞拉巴斯

+0

它不工作,alert(state.stateName);給出的輸出爲「undefined」 – 3bu1

+0

@Edgar:將'text'更改爲'json'然後您不必評估(數據) –

+0

我在'$ .each'部分進行了上面的編輯。現在應該工作 –

0

假設你可以發送數據爲json。你可以這樣做:

var Myselect = $('#state_ref'); 
$.getJSON("../Retrive_country?stateadd_1=none", 
    {countryREF : xyz } ,function(data){ 
    $.each(data, function(key, value) { 
    Myselect 
    .append($("<option></option>") 
    .attr("value",key) 
    .text(value)); 
    });//end each 
});//end get 

如果不是json格式,你能顯示你的數據是什麼樣子嗎?

+0

這是行不通的,我有回調「數據」中檢索到的所有信息我想要的是如何分離來自servlet.i的數據有三個字符串state,state_id,country_ref來自servlet,但他們都在一起進來回調函數我怎麼能再次分開它們,以便我可以在我的jsp中使用它們。 – 3bu1

+0

當你做console.log(數據)時,你看到了什麼?如果它的json那麼通常是data.state,data。state_id,data.country_ref會做。 –

+0

如果你可以告訴我console.log(data)的結果,我可以告訴你如何提取這些值。只是告訴我出來的數據格式。 –

0

請勿使用+PreparedStatement查詢!!

它適用於使用像

String sql1 = "SELECT * FROM state WHERE country_ref=?" 
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1) 
pst1.setInteger(0,country_ref); 
ResultSet j = pst1.executeQuery(); 

正是因爲SQL injection。如上所述,逃避該注射類型的最簡單方法是使用準備好的語句,正確地使用。請閱讀更多關於使用PreparedStatement

+1

這是一個真實的陳述,但這不是問題的答案。 –

相關問題