【发布时间】:2016-06-22 03:15:20
【问题描述】:
伙计们,我是新人,所以请帮助我.. 我正在尝试通过在 jsp 中使用 jqyery ajax 来获取 json 数据.. 但它不起作用.. 出现未定义.. 这是我的代码 在小服务程序中
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JSONObject json = null;
PrintWriter pw = response.getWriter();
try {
json = new JSONObject();
json.put("key1", "value1");
json.put("key2", "value2");
json.put("key3", "value3");
} catch (JSONException e) {
e.printStackTrace();
}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
pw.print(json.toString());
pw.close();
}
在jsp中
$(document).ready(function(){
$.ajax({
url : 'Server',
type: 'POST',
dataType : 'json',
error : function(that,e) {
console.log(e);
},
success : function(data) {
alert(data.json);
}
});
});
【问题讨论】:
-
解析错误是什么?
-
@bmarkham 我不知道.. 控制台显示解析错误
-
@bmarkham 你有什么建议??
-
我会检查 json 是否真的被发送到你拥有的任何 url
-
@bmarkham 好的......请告诉我......谢谢