[英]jquery form submition to a servlet
我正在使用jquery表單提交到servlet。 我將xyz的值發送到servlet,當我向xyz發出警報時,它顯示正確的值,但在回調函數中,數據警報顯示為null
。 這是我的劇本
<script>
$(document).ready(function(){
$("#country_id").change(function() {
var xyz = $("option:selected").val();
alert(xyz)
// var url ="../Retrive_country?stateadd_1=none&countryREF="+xyz;
$.get("../Retrive_country?stateadd_1=none",
{countryref : xyz } ,function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
//$(location).attr('href',url);
});
});
</script>
這是我的jsp
<html>
<body>
<div class="span2 clear">
<select name="country_id" id="country_id">
<option>-select-</option>
<option value="1" id="blabbb">1</option>
<option value="2" id="blabbb">2</option>
<option value="3" id="blabbb">3</option>
</select></div>
</body>
</html>
這是我的servlet。
String countryref= request.getParameter("countryREF");
String sql1 = "SELECT * FROM state WHERE country_ref="+countryref+" ";
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1);
ResultSet j = pst1.executeQuery();
while (j.next()) {
String 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(countryref);
我究竟做錯了什么?
您的服務器端代碼期望請求數據中包含countryREF
(區分大小寫),因此您可能只需要將$.get()
傳遞的數據更改為{ countryREF: xyz }
:
<script>
$(document).ready(function(){
$("#country_id").change(function() {
//var xyz = $("option:selected").val();
var xyz = $(this).val();
alert(xyz);
$.get("../Retrive_country?stateadd_1=none", { countryREF: xyz }, function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
從jQuery文檔中 ,您可以嘗試使用$.get
這種格式嗎?
$(document).ready(function () {
$("#country_id").change(function () {
var xyz = $("option:selected").val();
$.get("../Retrive_country?stateadd_1=none", {
countryref: xyz
})
.done(function (data) {
alert("Data Loaded: " + data);
});
});
});
要么:
$(document).ready(function () {
$("#country_id").change(function () {
var xyz = $("option:selected").val();
$.get("../Retrive_country?stateadd_1=none&countryref=" + xyz, function (data) {
alert("Data Loaded: " + data);
});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.