Status = parsererror, ajaxparsererror
Cause: the data returned by the servlet is not in Json format.
1. JS code:
1 var jsonStr = {'clusternum': 2, 'iterationnum': 3, 'runtimes ': 4}; 2 $. ajax ({3 type: "post", 4 // http: // 172.22.12.135: 9000/Json. json 5 url: "/LSHome", 6 dataType: 'json', 7 data: jsonStr, 8 success: function (data, textStatus) {9 if (textStatus = "success") {10 alert ("task created successfully" + data); 11} 12}, 13 error: function (xhr, status, errMsg) {14 alert ("task creation failed! "); 15} 16 });
2. Note that the above url is/LSHome (the project name is LSHome). Therefore, in the web. xml file, configure the Servlet as follows:
1 <servlet>2 <servlet-name>LSHomeServlet</servlet-name>3 <servlet-class>com.ys.servlet.LSHomeServlet</servlet-class>4 </servlet>5 <servlet-mapping>6 <servlet-name>LSHomeServlet</servlet-name>7 <url-pattern>/LSHome</url-pattern>
3. The Servlet code is:
1 protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {2 // number of clusters 3 String clusterNum = request. getParameter ("clusterNum"); 4 // number of iterations 5 String iterationNum = request. getParameter ("iterationNum"); 6 // Number of running times 7 String runTimes = request. getParameter ("runTimes"); 8 System. out. println ("cluster quantity:" + clusterNum + "--- iterations:" + iterationNum + "--- running times:" + runTimes); 9 PrintWriter out = response. getWriter (); 10 out. write ("success"); 11 out. close (); 12}
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.
1 PrintWriter out = response. getWriter ();
2 String jsonStr = "{\" success \ ": \" OK \"}";
3 out. write (jsonStr );
You can splice the returned values into JSON data format, and then whether status = parsererror is reported.