Skip to main content
 首页 » 编程设计

jquery之无法从 Flask 服务器获取 ajax 函数中的错误消息

2024年08月20日43yxwkf

在我的 Flask 应用程序中,我发送一条错误消息作为 AJAX 提交函数的响应,这样:

return jsonify(message='An error occurred!'),500 

在客户端我有这个功能:

 $("#submit_button").click(function(){ 
                ..... 
                $.ajax({ 
                    url: '/', 
                    data: $("#startcopy").serialize(), 
                    type: "POST", 
                    success: function(response) { 
                        console.log(response); 
                        $("#wait").hide(); 
                        alert(response); 
                        }, 
                    error: function(request,status, message) { 
                        console.log(request); 
                        $("#wait").hide();   
                        alert("Error\n"+message); 
                        } 
                    }); 
                } 
            }); 

但是我无法在警告框中显示错误消息。我哪里错了?

请您参考如下方法:

在错误回调中,响应主体将是第一个参数的属性,在 jQuery 文档中名为 jqXHR,在您的代码中名为 request。

既然你返回了 JSON,试试这个:alert("Error\n"+ request.responseJSON.message);