-1
發送JSON我寫這個servlet:通過AJAX請求和servlet的我回報他們從servlet的
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
}
發送數據。 的問題是,當我試圖讓我返回內部的servlet創建一個JSONObject給了我錯誤500
,讓我這個錯誤代碼:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
JSONObject json = new JSONObject();
json.put("name", "federico");
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
}
我requestAjax是:
$.ajax({
url: "ValidServlet",
type: 'POST',
data : JSON.stringify(submitFormData),
success: function (data) {
alert(data.name);
},
error: function (data) {
alert('error');
}
});
我的錯誤是:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) (17:33:34:492 | error, network)at http://localhost:8080/ProgettoProsa/ValidServlet>
也許我wron g返回所以json?
幫我找出我失敗的地方。
謝謝大家。
能否請您指定要接收,以及你發送請求的錯誤? –