For Servlet and Ajax interactions, the solution of status = parsererror is always reported. ajaxparsererror
Cause: the data returned by the servlet is not in Json format.
1. JS code:
Var jsonStr = {'clusternum': 2, 'iterationnum': 3, 'runtimes ': 4}; $. ajax ({type: "post", // http: // 172.22.12.135: 9000/Json. json url: "/LSHome", dataType: 'json', data: jsonStr, success: function (data, textStatus) {if (textStatus = "success ") {alert ("task creation succeeded" + data) ;}, error: function (xhr, status, errMsg) {alert ("task creation failed! ");}});
2. Note that the above url is/LSHome (the project name is LSHome). Therefore, in the web. xml file, configure the Servlet as follows:
<servlet> <servlet-name>LSHomeServlet</servlet-name> <servlet-class>com.ys.servlet.LSHomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LSHomeServlet</servlet-name> <url-pattern>/LSHome</url-pattern>
3. The Servlet code is:
Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// number of clusters String clusterNum = request. getParameter ("clusterNum"); // number of iterations String iterationNum = request. getParameter ("iterationNum"); // Number of running times String runTimes = request. getParameter ("runTimes"); System. out. println ("cluster quantity:" + clusterNum + "--- iterations:" + iterationNum + "--- running times:" + runTimes); PrintWriter out = response. getWriter (); out. write ("success"); out. close ();}
4. The result is always an error in the ajax method, and status = parsererror
xhr = Object {readyState: 4, responseText: "success", status: 200, statusText: "OK"}
5. solution:
The reason is that the data returned by the response object is incorrectly formatted and the correct method is used.
PrintWriter out = response.getWriter();String jsonStr = "{\"success\":\"OK\"}"; out.write(jsonStr);
You can splice the returned values into JSON data format, and then whether status = parsererror is reported.
The above is a small Editor to introduce you to the Servlet and Ajax interaction has always reported status = parsererror solution, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!