Home  >  Article  >  Web Front-end  >  What should I do if the post callback function in jquery does not execute?

What should I do if the post callback function in jquery does not execute?

coldplay.xixi
coldplay.xixiOriginal
2020-12-24 10:35:332669browse

The solution to the problem that the post callback function in jquery is not executed: JSON data must use double quotes. Since String cannot be nested with double quotes, just use the escape character. The code is [{\"hello\" :\"world\"}].

What should I do if the post callback function in jquery does not execute?

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.

Recommended: jquery video tutorial

Solution to the post callback function not executing in jquery:

1. Front-end code

$.post('${pageContext.request.contextPath}/user_deleteUser',{uid:row.uid},function(result){
     if (result.errorMsg){
         $.messager.show({    
             title: 'Error',
             msg: result.errorMsg
         });
     } else {
         $('#dg').datagrid('reload');    
     }
 },'json');

2. Backend code

public String deleteUser() {
        int count = userDao.deleteUser(model.getUid());
        try {
            PrintWriter writer = response.getWriter();
            if(count<=0) writer.write("{&#39;errorMsg&#39;:&#39;删除失败&#39;}");
            else writer.write("{&#39;success&#39;:&#39;删除成功&#39;}");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Obviously there is no problem with the frontend code, and there seems to be no problem with the backend code logically. Finally, Baidu learned that there was a problem with the JSON data format of the callback, which caused the callback The function has been unable to be executed. It turns out that JSON data must use double quotes!

我的:{&#39;hello&#39;:&#39;world&#39;}
标准:{"hello":"world"}

Since String cannot be used in nested double quotes, we can use escape characters

{\"hello\":\"world\"}

Related free learning recommendations: javascript(Video)

The above is the detailed content of What should I do if the post callback function in jquery does not execute?. 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