我想将JSON数据传递给ajax函数。但它让我解析异常
$(document).ready(function(){
$(".count").on('blur', function(){
var pack_count = $(this).val();
var order_id = $(this).attr('id');
$.post("order?action=update_counter&pack_count=" + pack_count + "&order_id=" + order_id, function(data){
alert("Change is successfull! Price: ");
$("#sum").html("Order Price: " + data);
var price = data['order_sum'];
var pack_count = data['pack_count'];
document.getElementById('price').value = price;
$(this).val(pack_count);
})
});
});
resp.setContentType("application/json; charset=UTF-8");
PrintWriter out = resp.getWriter();
String jsonData = "{ \"pack_count\" : \"" + selectedOrder.getPackageCount() + "\", \"order_sum\" : \"" + countSum() + "\" }";
out.print(jsonData);
解决了!感谢printfmyname
和backslash条评论。刚刚将jsonData更改为
String jsonData = "{ " + "\"pack_count\" : " + selectedOrder.getPackageCount() + ", \"order_sum\" : " + countSum() + " }";
答案 0 :(得分:1)
你要输出一个json字符串输出,但你的标题默认设置为text / html或不是json的东西。因此,在写入输出
之前,需要设置正确的标题response.setContentType("application/json; charset=UTF-8"); # you may get rid of ; charset=UTF-8 as it is not critical
然后当您完成写入输出调用
out.flush();
更新
上有错误
String jsonData = "{ \"pack_count\" : \"" + selectedOrder.getPackageCount() + ", \"order_sum\" : " + countSum() + " }";
您在selectedOrder.getPackageCount()之后缺少引号,更改为follwong
String jsonData = "{ \"pack_count\" : \"" + selectedOrder.getPackageCount() + "\", \"order_sum\" : " + countSum() + " }";