Home  >  Article  >  Web Front-end  >  What should I do if the jQuery Ajax Post callback function does not execute?

What should I do if the jQuery Ajax Post callback function does not execute?

coldplay.xixi
coldplay.xixiOriginal
2021-01-20 10:49:172861browse

The JSON data format problem of the callback has caused the callback function to be unable to be executed; the solution to the jQuery Ajax Post callback function not being executed: use double quotes for JSON data, use escape characters to escape String, the code is [ {\"hello\":\"world\"}].

What should I do if the jQuery Ajax Post callback function does not execute?

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer. This method is suitable for all brands of computers.

Recommended: jquery video tutorial

##Solution to jQuery Ajax Post callback function not executing:

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. Back-end 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 nothing wrong with the front-end code, and there seems to be no logical problem with the back-end code. Finally Baidu learned that there was a problem with the JSON data format of the callback, which caused the callback function to be 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\"}

You’re done!


Related free learning recommendations: js video tutorial

The above is the detailed content of What should I do if the jQuery Ajax Post callback function 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