Ajax 结果打印 [object HTMLInputElement]
Ajax result print [object HTMLInputElement]
我想使用 ajax 接收数据。但是当使用这段代码时,结果打印 [object HTMLInputElement]。我可以将对象更改为字符串吗?
这是我在 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>
而这是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 {
}
谢谢。
我从来没有在 JSP 中编码,所以请多关照。
试试这个。它基于这个例子。 http://codesstore.blogspot.com/2011/12/json-with-jquery-jsp-servlets.html
基本上你想找到一种方法来 return 一个 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
确保导入以获得 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 {
}
将您的服务器代码更改为此...
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 {
}
还有你的客户端代码...
$('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]);
}
});
});
试试这个更新。在ajax,成功块:
success : function(data, textStatus, jqXHR){
$('#color').val(data.color);
$('#price').val(data.price);
}
add_products.jsp:
在 jsp 顶部的这一行。
<%@ page contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>
输出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("}");
我想使用 ajax 接收数据。但是当使用这段代码时,结果打印 [object HTMLInputElement]。我可以将对象更改为字符串吗?
这是我在 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>
而这是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 {
}
谢谢。
我从来没有在 JSP 中编码,所以请多关照。 试试这个。它基于这个例子。 http://codesstore.blogspot.com/2011/12/json-with-jquery-jsp-servlets.html
基本上你想找到一种方法来 return 一个 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
确保导入以获得 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 {
}
将您的服务器代码更改为此...
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 {
}
还有你的客户端代码...
$('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]);
}
});
});
试试这个更新。在ajax,成功块:
success : function(data, textStatus, jqXHR){
$('#color').val(data.color);
$('#price').val(data.price);
}
add_products.jsp:
在 jsp 顶部的这一行。
<%@ page contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>
输出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("}");