[Answer]-Not stisfying the condition if request.is_ajax(): in django view

1👍

Yes, it’s an issue with cross domain requests. Normally the server has to tell you which HTTP headers it accepts when you are doing CORS. In order to use .is_ajax() the request has to contain

X-Requested-With: XMLHttpRequest

header (that’s exactly what .is_ajax() checks). But by default the HTTP standard forbids to send that header via CORS. So to make it work your server not only has to send Access-Control-Allow-Origin header but also

Access-Control-Allow-Headers: X-Requested-With

Then you have to set that header in your request manually:

$.ajax({
    // some settings
    headers: {'X-Requested-With': 'XMLHttpRequest'}
});

EDIT I’ve just realized that you are actually doing JSONP, which is not CORS. In that case there is no way to set that header since JSONP is not AJAX at all. It is only wrapped to look like AJAX, but what actually happens under the hood is that the browser creates a <script> tag that points to the URL. It is impossible to change that. I advice switching to CORS. Other possibility is to send additional info in URL (as a query string). Not elegant but it will work.

Leave a comment