goodquestion
goodquestion

Reputation: 11

type error when excuting ajax code in django

I am using jquery ajax to get json data into my webpage (Django framework). I can not seem to render the json data (my first step is simply to do a console.log(data)-not working). I am getting a type "unknown" so I think that may be part or all of the problem. In my simple template, I have a button that makes an ajax call which is not working. Code below includes my urls.py file, views.py file with the relevant function, and the ajax code on my website. The network tab on the firefox developers page says code 200, I am assuming this means that my urls.py and views.py were correct, but please let me know if this is true. My app is on pythonanywhere. I am using console.log to if the ajax call can bring up the json data. It did not. My javascript triggers the error code--not sure whey. Thank you for any help you may provide.

urls.py(see last url):

app_name = 'beach'

urlpatterns = [
    url(r'^$', views.index,name='index'),  #when you go to the beach directory, it looks at this urls.py file, this says go to views.py in this folder, index function
    url(r'^(?P<lakes>[a].*)/$', views.searched, name='lakes'),  #regex = starts with a letter then it can be anything
    url(r'^(?P<gnisid>[0-9]+)/$', views.gnis, name='GNIS'),  #regex = numbers ony, many.  name-'GNIS' a link to the HTTPResponseRedirect or directly from the template from a link
    url(r'^(?P<tmap>tmap)/$', views.tmap, name='tmap'),
    url(r'^(?P<profile>profile)/$', views.profile, name='profile'),
    url(r'^(?P<ajax>profiles/default)/$', views.defaultajax, name='ajaxgnis')   
]

views.py

def defaultajax(request, ajaxgnis):
    x = {"foo": "bar"}
    jsondata = json.dumps(x)
    return HttpResponse(jsondata, content_type='application/json')

template with the javascript code (lots of testing, sorry)

window.onload = function () {

    $('#putprofile').click(makeProfile);

    function makeProfile(){
        $.ajax({
                url: "/beach/profile/default/",
                dataType: "json",

                error: function(jqXHR, textStatus, errorThrown) {
                    alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');

                    $('#result').html('<p>status code: '+jqXHR.status+'</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
                    console.log('jqXHR:');
                    console.log(jqXHR);
                    console.log('textStatus:');
                    console.log(textStatus);
                    console.log('errorThrown:');
                    console.log(errorThrown);
                    console.log('datatype:');
                    console.log(typeof data);
                },

                success: function(data, textStatus, jqXHR) {
                    alert('Load was performed. Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information! ');
                    console.log('jqXHR:');
                    console.log(jqXHR);
                    console.log('textStatus:');
                    console.log(textStatus);
                    console.log('data:');
                    console.log(data);
                }
            });

    }
}

Upvotes: 0

Views: 91

Answers (1)

goodquestion
goodquestion

Reputation: 11

I figured out my problem:

(1) I had an existing url named profile, and my original ajax call was /profile/default, I think this caused some problems with the urls in the urls.py. I changed the ajax call url in my template to /beach/profiles/default, beach is my app name. Final url in my urls.py file was: url(r'^(?P<defaultajax>profiles/default)/$', views.defaultajax, name='ajaxgnis'). Note again that the "/" in/beach/proiles/default` was critical since without the "/" the url would start at the root "beach" directory.

(2) I had an error in my named group not matching the variable in my views.

(3) I caused myself confusion by not "reloading" the app in the web tab of pythonanywhere.

Upvotes: 1

Related Questions