Django,Ajax和GET请求


                    

这里有很多类似的问题,但是我无法解决我遇到的问题。 从理论上讲情况很简单-我将json发送到Django视图,然后收到json响应。 我是用python,Django和REST框架完成的。 但是使用Ajax我做不到,URLconf和ajax的相对url有点问题,我无法弄清楚什么地方出了问题以及如何使其工作。 我的意思是,通过一些URL conf,我可以使用chrome插件发送请求,并且我需要一个响应,但是ajax的conf却得到404。 如果我更改它,Django开始返回我的html页面,而不是json响应。 我在这里想念什么?
我的Ajax请求:</ p>

  $。ajax({
         网址:“ ajax / apply_city /”,
         输入:“ GET”,
         数据:{
             'city':obj.textContent
         },
         dataType:'json',
         成功:功能(数据){
             var str ='';
             data.districts.forEach(函数(区){
                 str + =''+区+'';
             });
             window.alert(str);
             document.getElementById(“ district_dropdown”)。innerHTML = str;
         },
     });
</ code> </ pre>

适用于所有请求但不包含ajax的网址:</ p>

  re_path('^ index / $',views.index,name ='index'),
     re_path('^ index / ajax / apply_city /',views.apply_city,name ='apply_city'),
</ code> </ pre>

当ajax发出请求时,日志显示未找到:/ index /...</ code>和 GET / index / ... 404 </ code>。 我已经看到,对于ajax,我不应该使用 $ </ code>符号关闭url。 如果执行 ^ index / </ code>,我将获得HTML页面作为响应(但使用HTTP200)。 也许这是Django的问题,或者我不知道如何正确执行。 如果有人知道如何解决,请提供帮助。</ p>
     </ div>

展开原文

原文

There are plenty of similar questions here but I can't solve that problem I have. Situation is simple in theory - I send json to Django view, I get a json response. I did it with python, with Django, with REST framework. But with Ajax I can't do it, there's something with URLconf and ajax's relative url kind of stuff and I can't figure out what's wrong and how to make it work. I mean with some URL confs I can send a request with my chrome plugin and I have a response I need, but ajax is getting 404 with that confs. If I change it Django starts to return my html page instead of json response. What am I missing here? My ajax request:

$.ajax({
        url: 'ajax/apply_city/',
        type: 'GET',
        data: {
            'city': obj.textContent
        },
        dataType: 'json',
        success: function(data) {
            var str = '';
            data.districts.forEach(function (district) {
                str += '<a class="dropdown-item" href="#" onclick="applyDistrict(this)">' + district + '</a>';
            });
            window.alert(str);
            document.getElementById("district_dropdown").innerHTML = str;
        },
    });

urls that works with requests from everything but not ajax:

    re_path('^index/$', views.index, name='index'),
    re_path('^index/ajax/apply_city/', views.apply_city, name='apply_city'),

Logs show Not Found: /index/... when ajax makes request and GET /index/... 404. I've seen that for ajax I shouldn't close url with $ sign. If I do ^index/ I get HTML page as a response (but with HTTP200). Maybe that's some Django issue or I don't understand how to do it properly. If anyone knows how to fix it please help.

1个回答


可能是您输入了错误的URL。 您可以尝试如下更改ajax中的URL:</ p>

  url:“ {%url'apple_city'%}”,
</ code> </ pre>
     </ div>

展开原文

原文

It's probably that you put the wrong URL. You can try changing the URL within ajax just like below:

url: "{% url 'apple_city' %}",

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐