簡體   English   中英

在Django中執行Ajax代碼時出現類型錯誤

[英]type error when excuting ajax code in django

我正在使用jquery ajax將json數據獲取到我的網頁(Django框架)中。 我似乎無法呈現json數據(我的第一步只是做一個console.log(data)-不起作用)。 我得到的是“未知”類型,因此我認為這可能是部分或全部問題。 在我的簡單模板中,我有一個無法進行Ajax調用的按鈕。 下面的代碼包括我的urls.py文件,具有相關功能的views.py文件以及我網站上的ajax代碼。 firefox開發人員頁面上的網絡選項卡上顯示代碼200,我認為這意味着我的urls.py和views.py是正確的,但是請告訴我這是否正確。 我的應用程序在pythonanywhere上。 我正在使用console.log來檢查ajax調用是否可以調出json數據。 它沒。 我的JavaScript觸發了錯誤代碼-不確定乳清。 感謝您提供的任何幫助。

urls.py(請參閱最后一個網址):

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')

javascript代碼的模板(大量測試,抱歉)

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);
                }
            });

    }
}

我發現了我的問題:

(1)我有一個名為profile的現有URL,而我最初的ajax調用是/ profile / default,我認為這導致urls.py中的URL出現了一些問題。 我將模板中的ajax調用網址更改為/beach/profiles/default ,beach是我的應用名稱。 我的urls.py文件中的最終URL是: url(r'^(?P<defaultajax>profiles/default)/$', views.defaultajax, name='ajaxgnis'). Note again that the "/" in url(r'^(?P<defaultajax>profiles/default)/$', views.defaultajax, name='ajaxgnis'). Note again that the "/" in / beach / proiles / default`中的“ /”非常重要,因為如果沒有“ /”,則URL將從根“ beach”目錄開始。

(2)我的命名組中有一個錯誤,與我的視圖中的變量不匹配。

(3)我沒有通過在pythonanywhere的Web選項卡中“重新加載”應用程序而引起混亂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM