My Ajax request throws an error in django, how do I find what it is?

爷,独闯天下 提交于 2019-12-11 04:02:20

问题


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.



来源:https://stackoverflow.com/questions/6593394/my-ajax-request-throws-an-error-in-django-how-do-i-find-what-it-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!