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

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

coldplay.xixi
Release: 2022-12-30 11:12:15
Original
2864 people have browsed it

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');
Copy after login

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;
    }
Copy after login

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"}
Copy after login

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

{\"hello\":\"world\"}
Copy after login

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!

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!