4

Here is my js that uses Jquery blockUI plug-in:

$(document).ajaxStop($.unblockUI);

 $('#submit-id-submit').click(function() {
  $.blockUI({message:'<h1><img src="{% static 'css/spinner.gif' %}" /> Just a moment ...</h1>'});
  $.ajax({
    url: "/search/test/",
    cache:'false',
    dataType: 'text',
    type:"GET",
    success: function(data){
      alert(data);
    },
    error: function(data){
      alert('error; '+ eval(error));
    }
  });
});

my view:

def test_ajax(request):
    time.sleep(20)
    print "in test_ajax"
    return HttpResponse("hell world")

url(r"search/test/$", test_ajax,name="dummy"),

First, I see the ajax call is returning error (because I get alert from error. but it does not show the error message)

Secondly, my view test_ajax is not called, because I would expect the print statement there to be executed, but it does not execute.

I cannot figure out what is going wrong here.

1
  • Can you place a breakpoint on your alert, say in Chrome's dev tools, to inspect the error and post it? Commented Apr 25, 2014 at 23:11

2 Answers 2

8
+100

I have had similar issues in the past. The reason is you are allowing the default form submission. Try this and see what you get;

$('#submit-id-submit').click(function(e) {
   e.preventDefault();
  $.blockUI({message:'<h1><img src="{% static 'css/spinner.gif' %}" /> Just a moment ...</h1>'});
  $.ajax({
    url: "/search/test/",
    cache:'false',
    dataType: 'text',
    type:"GET",
    success: function(data){
      alert(data);
    },
    error: function(data){
      alert('error; '+ eval(error));
    }
  });
});

or simply return false

$('#submit-id-submit').click(function() {

  $.blockUI({message:'<h1><img src="{% static 'css/spinner.gif' %}" /> Just a moment ...</h1>'});
  $.ajax({
    url: "/search/test/",
    cache:'false',
    dataType: 'text',
    type:"GET",
    success: function(data){
      alert(data);
    },
    error: function(data){
      alert('error; '+ eval(error));
    }
  });
   return false;
});

By doing either of this, you prevent default action (in your case form submission I guess) you can read more about this here;

Sign up to request clarification or add additional context in comments.

Comments

1

Your error function is trying to alert the error variable which doesn't exist. Try alerting the data variable.

Also to help troubleshoot you could check the view function works with a non AJAX GET request.

Lastly this doesn't apply in your case as your using a GET request, but if you need to do an AJAX post request you'll need to send the CSRF token in the header https://docs.djangoproject.com/en/1.6/ref/contrib/csrf/#ajax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.