简体   繁体   中英

Ajax result print [object HTMLInputElement]

I want to receive data using ajax. But when use this code, result prints [object HTMLInputElement]. Can I change object to string?

Here's My code in JSP what use ajax.

$('select#product').change(function() {

        var param = "code=" + $('#product').val();

        $.ajax({
            url : 'add_products/add_products.jsp',
            contentType : "application/x-www-form-urlencoded; charset=UTF-8",
            data : param,
            type : 'POST',
            dataType : 'text',
            success : function(data, textStatus, jqXHR){
                $('#color').val(color);
                $('#price').val(price);
            }
        });
    });
...
<td>
    <input type="text" id="color" class="form-control" name="color"  />
</td>
<td>
    <input type="text" id="price" class="form-control" name="price" value="0"  />
</td>

And This is add_products.jsp what receive the upper jsp.

product_code = request.getParameter("code");

try {
    query = "select * from new_product where product_code='"+product_code+"'";
    rs = stmt.executeQuery(query);
    while (rs.next()) {
        size = rs.getString("sizes");
        color = rs.getString("color");
        price = rs.getString("price_cny");

        out.println(color);
        out.println(price);

    }
} catch (SQLException e) {
    out.println(e);
} finally {
}

Thanks.

Change your server code to this...

product_code = request.getParameter("code");

try {
    query = "select * from new_product where product_code='"+product_code+"'";
    rs = stmt.executeQuery(query);
    while (rs.next()) {
        size = rs.getString("sizes");
        color = rs.getString("color");
        price = rs.getString("price_cny");

        out.println(color+"_"+price);                //concatenate 2 values

    }
} catch (SQLException e) {
    out.println(e);
} finally {
}

And your client code to this...

$('select#product').change(function() {

        var param = "code=" + $('#product').val();

        $.ajax({
            url : 'add_products/add_products.jsp',
            contentType : "application/x-www-form-urlencoded; charset=UTF-8",
            data : param,
            type : 'POST',
            dataType : 'text',
            success : function(data, textStatus, jqXHR){
                var values = data.split("_");       //get your 2 values seperated
                $('#color').val(values[0]);
                $('#price').val(values[1]);
            }
        });
    });

I have never coded in JSP so be kind. Try this. It is based on this example. http://codesstore.blogspot.com/2011/12/json-with-jquery-jsp-servlets.html

Basically you want to find a way to return a json object.

Ajax

$('select#product').change(function() {

  var param = "code=" + $('#product').val();

  $.ajax({
    url : 'add_products/add_products.jsp',
    contentType : "application/x-www-form-urlencoded; charset=UTF-8",
    data : param,
    type : 'POST',            
    success : function(data, textStatus, jqXHR){

      try {
        $result = $.parseJSON(data);
      }
      catch(error)
      {
        alert('Error parsing json ' + data);
      }

      alert(data.color);
      alert(data.price);

    }
  });
});

JSP

Make sure you import for json support.

import org.json.JSONObject;  
product_code = request.getParameter("code");

try {
    query = "select * from new_product where product_code='"+product_code+"'";
    rs = stmt.executeQuery(query);

    JSONObject json = new JSONObject();  

    while (rs.next()) {
        size = rs.getString("sizes");
        color = rs.getString("color");
        price = rs.getString("price_cny");

        json.put("color", color);  
        json.put("pr", price);  
    }

       out.println(json);
} catch (SQLException e) {
    out.println(e);
} finally {
}

Try this updates. In ajax, success block:

success : function(data, textStatus, jqXHR){
     $('#color').val(data.color);
     $('#price').val(data.price);
}

add_products.jsp :

At this line at the top of jsp.

<%@ page contentType="application/json; charset=UTF-8"
    pageEncoding="UTF-8"%>

Output jdbc result as:

out.print("{");
if (rs.next()) {//note, replaced with while loop
    out.print("\"sizes\":" + rs.getString("sizes") + ",");
    out.print("\"color\":" + rs.getString("color") + ",");
    out.print("\"price\":" + rs.getString("price_cny"));
}
out.print("}");

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