[Answered ]-Django – is_ajax() not returning true

0👍

✅

Don’t use the onclick attribute of the button, it may not catch everything. Use jQuery’s submit() event handler (http://api.jquery.com/submit/) instead.If you don’t see any request in the console, then Daniel is very likely right: the form is submitted the usual way.

2👍

is_ajax() checks X-REQUESTED-WITH header. Usually jQuery sets it properly. You can try to explicitly set this header in $.ajax:

$.ajax({
   url: form.action,
   type: 'get',
   success: function(response){
      $("#upd-main").load(response); 
   },
   headers: {
     'X-Requested-With': 'XMLHttpRequest'
   }
});

If this does not work, it means that your server or proxy strips this header.

0👍

You’re not disabling the default action of the button, so the form is getting submitted by the browser.

Since you’ve explicitly put the JS function in the HTML onclick, you can just do this:

<button ... onclick="update_profile_page();return false;"

Leave a comment