简体   繁体   中英

jquery form submition to a servlet

I am using jquery form submission to a servlet. .I am sending value of xyz to servlet,when i alert xyz it shows the correct value but in the callback function data alert shows as null . here is my script

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

here is my 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>

here is my 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);

What am i doing wrong?

Your server-side code is expecting countryREF (case-sensitive) in the request data so you probably just need to change your data passed in your $.get() to { 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>

From the jQuery documentation , can you try this format of $.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);
        });
    });
});

OR:

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

        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM