The expected error is one of the servers that I expected or even raised myself in the code. For example, when a user tries to perform an action for which he does not have sufficient privileges, I would raise a PermissionError (user-defined Exception ) error message.
I was looking for a good way to handle the expected error for an AJAX situation. The only requirement is to be able to display an error message to the user, since I want my users to be aware of what is happening.
My current approach is to pack the error message into a JSON object and send it back to the client end
var ajaxResponse = $.ajax({ .... }); ajaxResponse.done(function(jsonObj) { if (jsonObj.success) { } else { } }); ajaxResponse.fail(function(jqXHR, textStatus, errorThrown) { });
I have a different approach that I'm not sure about. Basically, instead of packing the expected error message in a JSON object, in my django code I would return HttpResponse(content="no sufficient privilege", status=403) . JQuery at the client end will be modified as follows:
ajaxResponse.done(function(response_data) { }); ajaxResponse.fail(function(jqXHR, textStatus, errorThrown) { });
I like how the second approach groups all errors, expected or unexpected, in one place. But then I feel that the http status code should not be used that way. In any case, I want to know which one is the right way. If none of them, please share what you would do.
jquery python ajax django
tamakisquare Apr 20 2018-12-21T00: 00Z
source share