Home > Web Front-end > JS Tutorial > body text

Solution to the problem that status=parsererror always reports when Servlet interacts with Ajax

亚连
Release: 2018-05-22 16:17:15
Original
1718 people have browsed it

This article mainly introduces the solution to the problem of always reporting status=parsererror when Servlet interacts with Ajax. It is very good and has reference value. Friends in need can refer to it

Reason: The data returned by the servlet is not in Json format

##1. The JS code is:

var jsonStr = {'clusterNum':2,'iterationNum':3,'runTimes':4};
    $.ajax({
      type: "post",
      //http://172.22.12.135:9000/Json.json
      url: "/LSHome/LSHome",
      dataType : 'json',
      data : jsonStr,
      success: function(data,textStatus){
        if(textStatus=="success"){ 
          alert("创建任务操作成功"+data);      
        }        
      },
      error: function(xhr,status,errMsg){
        alert("创建任务操作失败!");
      }
    });
Copy after login

2. Note that the above url is /LSHome/LSHome, (the project name is LSHome), so 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>
Copy after login

3. The code in the Servlet is:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //聚类数量
    String clusterNum = request.getParameter("clusterNum");
    //迭代次数
    String iterationNum = request.getParameter("iterationNum");
    //运行次数
    String runTimes = request.getParameter("runTimes");
    System.out.println("聚类数量为:"+clusterNum+"---迭代次数:"+iterationNum+"---运行次数:"+runTimes);
    PrintWriter out = response.getWriter();      
    out.write("success");
    out.close();  
  }
Copy after login

4. The result is that it always enters ajax error in the method, and status=parsererror

xhr = Object {readyState: 4, responseText: "success", status: 200, statusText: "OK"}
Copy after login

5. Solution:

The reason is that the data format returned through the response object is incorrect. The correct method is

 PrintWriter out = response.getWriter();
String jsonStr = "{\"success\":\"OK\"}";
 out.write(jsonStr);
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. helpful.

Related articles:

PHP ajaxDetailed explanation of the case of obtaining news data

##PHP AJAX How to Implementing the voting function


How does PHP get the headers (case) in

ajax

The above is the detailed content of Solution to the problem that status=parsererror always reports when Servlet interacts with Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!