For Servlet and Ajax interactions, the solution of status = parsererror is always reported. ajaxparsererror

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.