Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get error message in ajax function from Flask server

In my Flask applòication, I am sending an error message as response from an AJAX submit funciton, this way:

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

In the client I have this function:

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

But I can't get the error message displayed in the alert box. Where am I wrong?

like image 351
Fabio Marzocca Avatar asked Jul 15 '26 15:07

Fabio Marzocca


1 Answers

In the error callback, the response body will be a property of the first argument, which in the jQuery docs is named jqXHR and in your code is named request.

Since you returned JSON, try this: alert("Error\n" + request.responseJSON.message);

like image 133
saltire Avatar answered Jul 18 '26 06:07

saltire