[英]Ajax result print [object HTMLInputElement]
I want to receive data using ajax. 我想使用ajax接收数据。 But when use this code, result prints [object HTMLInputElement]. 但是,当使用此代码时,结果将打印[object HTMLInputElement]。 Can I change object to string? 我可以将对象更改为字符串吗?
Here's My code in JSP what use ajax. 这是我在JSP中使用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. 而且这是add_products.jsp接收到的上级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. 我从来没有用JSP编写过代码,所以请客气。 Try this. 尝试这个。 It is based on this example. 它基于此示例。 http://codesstore.blogspot.com/2011/12/json-with-jquery-jsp-servlets.html http://codesstore.blogspot.com/2011/12/json-with-jquery-jsp-servlets.html
Basically you want to find a way to return a json object. 基本上,您想找到一种返回json对象的方法。
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 JSP
Make sure you import for json support. 确保导入以获取json支持。
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: 在ajax中,成功块:
success : function(data, textStatus, jqXHR){
$('#color').val(data.color);
$('#price').val(data.price);
}
add_products.jsp : add_products.jsp :
At this line at the top of jsp. 在此行中,jsp的顶部。
<%@ page contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>
Output jdbc result as: 将jdbc结果输出为:
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("}");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.