servlet在浏览器可以访问 为什么 jquery 的ajax访问失败
write完之后,调用writer.flush()以及writer.close();
前端ajax在url下加上type:"application/json"
https://www.jianshu.com/p/2759e1a6b2d5
$.ajax({
/*
type: 请求的类型
dataType: 从服务端接收的数据类型 html, xml, text, json
url: 请求路径
data: 请求参数
success: 回调函数 msg: 从服务端接收过来的内容
*/
type: "post",
dataType: "json",
url: "${pageContext.request.contextPath}/JsonServlet",
data: person,
success: function (msg) {
alert(msg);
}
});
ajax参数没写全,提交get方式没写
2.response.getWriter().print(jsonStr);是print不是write,加上 dataType: "json",
参数没写全吧,目测的话。
是不是请求类型不匹配啊?
ajax默认http请求方式为GET,请楼主贴一下后台错误日志代码,如果没有报错的话,那么就确定是js解析数据的问题,有可能js想要封装json数据,但是出现了格式错误。楼主可以换一个返回值试试