2

Trying to simply make an ajax call to my view and get the template html back.

$(document).ready(function () {

    $.ajax({
        url:url,
        data:{language:'English'},
        dataType:'html',
        success:function (data, status, xhr) {
            alert('works yea');
            $('#profiles-section').html(data);
        }
       error: function(data){
            alert('errors');
        }
    });
});

My test view which I have verified has the correct url is never called, I have set break points and nothing happens, I am alerting the url on the client side before I make the ajax call and it should match what I have defined in my urls.py.

def teset_view(request, username):
   template = 'profiles_view.html'
   return render_to_response(template,
        context_instance=RequestContext(request))

Have I missed something simple?

UPDATE:

I added the error to see if it would error and I receive this alert, how can I figure out why jquery errors like this. I don't have any other conflicting places where I would cross reference

It looks like I am running into a jqXHR status always returning zero

error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            }

What can I do to fix this, I have tried adding a e.preventDefault at the end of the on click method

UPDATE

My url is set up this way

url(r'^profiles/(?P<username>[\w.@+-]+)/$','Taleebo.views.profiles_view',name="profiles"),

, i have added the getCookie() javascript safe method so i can make proper ajax request.

Warz
  • 7,386
  • 14
  • 68
  • 120
  • did you try adding the `type: "GET"` parameter to your $.ajax function? – Viren Rajput Feb 07 '13 at 03:11
  • yes i did. I have updated my question. to show that it alerts on error, with no help from jquery on why – Warz Feb 07 '13 at 03:15
  • try console.log(data) in your error handler function – Viren Rajput Feb 07 '13 at 03:18
  • I don't think django lets you make requests unless you add the csrf token in the request parameters. Get the `csrftoken` cookie following this method and request with it. https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax – Bibhas Debnath Feb 07 '13 at 06:37
  • run the site in the dev server and watch the terminal output, what does it say when you send the ajax request? – jsj Feb 07 '13 at 06:40
  • @Bibhas I'm pretty sure you only need the csrf token for POST. Well I have a django project with AJAX and all the AJAX requests are GET and I never need the token. – jsj Feb 07 '13 at 06:44
  • Could you update your question with url (from ajax call) and url sytax from urls.py? – Lukasz Koziara Feb 07 '13 at 08:11
  • @Warz do you have [this](https://gist.github.com/miki725/3892061) added in your JS? – Jonathan Feb 07 '13 at 09:42
  • @Jonathan i will add this and post my results. – Warz Feb 07 '13 at 17:17
  • I have added this now with no update, i still get the same error – Warz Feb 08 '13 at 01:59

2 Answers2

1

You have some errors. Install firebug and you will see where they are!

ndpu
  • 22,225
  • 6
  • 54
  • 69
0

Unbelievable. Found out that my link was canceling the ajax request.

Simply adding return false after the on click method fixes it.

Warz
  • 7,386
  • 14
  • 68
  • 120