My Ajax request throws an error in django, how do

2019-07-18 20:45发布

问题:

How do I get more information about the errors in my Ajax requests?

When error occurs in Django during a normal HTTP request, Django shows a descriptive error page with exception details.

However, when error occurs during an AJAX request, I just know the HTTP error code and that's it. I'd like to know as much details as I learn from a Django error page.

回答1:

Your web server's error log will contain additional information about any uncaught exceptions.



回答2:

Handle errors within your ajax request and use responseText to get the response text and put it into an empty div that you have created for that.

For example:

$.ajax({
        url:"{% yourView %}",
        dataType: 'json',
        success: function(jsonData){
            alert("Hooray!");
        },
        error: function(jqXHR, textStatus, errorThrown){
            $('.errors_div').html(jqXHR.responseText);
        }
    });

The django error page will be rendered into div class=errors_div



回答3:

Use Firebug. In Console you'll see the AJAX request, just right click -> Copy Response Content and save that as an HTML file.



回答4:

Why not access the url directly in your browser's address bar so you see a normal traceback?

Wrap it in another view to change the MIME type to text/html.

I also often have a view that wraps the JSON output in a html tag so the Django Debug Toolbar works correctly.