【发布时间】:2013-09-18 21:03:02
【问题描述】:
这是java代码;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,Object> hsssmap= new HashMap<String,Object>();
hsssmap.put("a","true");
hsssmap.put("b","true");
write(response,hsssmap);
}
private void write(HttpServletResponse response, Map<String,Object> hsssmap) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Gson gson = new Gson();
String json = gson.toJson(hsssmap);
response.getWriter().write(json);
}
这里是js代码;
$(document).ready(function(){
$.ajax({
url: 'uri',
type: 'POST',
dataType: 'json',
data: data,
success: function(data){
$.each (data, function (key,value) {
alert(key+" "+value);
});
},
error: function (e) {
alert("error : " + e);
}
});
return false;
});
成功事件不起作用。如何解决?
【问题讨论】:
-
你的java代码被执行了吗?您是否更改了响应类型?如果您在
$.ajax方法中遇到错误,即如果错误函数被命中,错误消息是什么? -
我对java一无所知,但是当服务器响应不是200 OK时成功功能不起作用
-
'uri' 不是有效资源...您需要为 url 参数指定有效资源。
-
java 代码运行正常。在写函数的最后,System.out.println(json)= {"id":"true","password":"true"}
-
成功事件不起作用是什么意思?它有什么作用?尝试将错误函数更改为 alert("error:" + e.responseText);
标签: java jquery ajax response touchatag