Home  >  Article  >  Web Front-end  >  How to solve the status=parsererror error reported during Ajax interaction

How to solve the status=parsererror error reported during Ajax interaction

php中世界最好的语言
php中世界最好的语言Original
2018-04-02 13:33:324737browse

This time I will show you how to solve the status=parsererror error reported during Ajax interaction. What are the precautions to solve the status=parsererror error reported during Ajax interaction. The following is a practical case. Let’s take a look. .

Cause: 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("创建任务操作失败!");
      }
    });

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>

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();  
  }

4. The result is always an error entering the ajax method, and status=parsererror

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

5. Solution:

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

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

can piece together the return value into JSON data format , and then will it report status=parsererror

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use ajax to realize pop-up login

##Ajax+bootstrap steps to optimize web user experience

The above is the detailed content of How to solve the status=parsererror error reported during Ajax interaction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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