【问题标题】:where is the error in my ajax script that return NoneType to the views.py我的 ajax 脚本中返回 NoneType 到 views.py 的错误在哪里
【发布时间】:2019-07-16 19:23:57
【问题描述】:

我有一个 ajax 脚本,它使用 GET 方法将下拉列表中的选定选项发送到 django 函数,我以 json 格式发送数据

一旦我从下拉列表中选择一个选项,它就会显示以下错误:

文件“C:\Users\LT GM\Desktop\test2ForImportExport\test2\testpro\views.py”,第 30 行,在 获取详细信息 answer = str(country_name[1:-1]) TypeError: 'NoneType' object is not subscriptable

home2.html

<html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
                <script>
             $(document).ready(function(){
             $('select#selectcountries').change(function () {
                 var optionSelected = $(this).find("option:selected");
                 var valueSelected  = optionSelected.val();
                 var country_name   = optionSelected.text();


                 data = {'cnt' : country_name };
                 alert(country_name);
                 $.ajax({
                     type:"GET",
                     url:'/getdetails',
                     data:JSON.stringify(data),
                     success:function(result){
                        console.log(result);
                        $("#selectcities option").remove();
                        for (var i = result.length - 1; i >= 0; i--) {
                            $("#selectcities").append('<option>'+ result[i].name +'</option>');
                        };
                      },
                });
            });
        });
        </script>
    </head>

    <body>
        <select name="selectcountries" id="selectcountries">
        {% for item in countries %}
            <option val="{{ item.name }}"> {{ item.name }} </option>    
        {% endfor %}
        </select>   


        <select name ="selectcities" id="selectcities">


        </select>       

        <select name ="selectroads" id="selectroads">


        </select>



    </body>
</html>

views.py

def home2(request):
    countries = country.objects.all()
    print(countries)
    return render(request, 'home2.html',{'countries': countries})



def getdetails(request):
    if request.method == 'GET' and request.is_ajax():
        country_name = request.GET.get('cnt', None) 
        print ("ajax country_name ", country_name)

        result_set = []
        all_cities = []

        answer = str(country_name[1:-1])
        print('answer = ' ,answer)
        selected_country = country.objects.get(name=answer)
        print ("selected country name ", selected_country)

        all_cities = selected_country.city_set.all()
        for city in all_cities:
            print ("city name", city.name)
            result_set.append({'name': city.name})

        return HttpResponse(simplejson.dumps(result_set),content_type='application/json')
    else:
        return redirect('/')

【问题讨论】:

    标签: jquery json ajax django


    【解决方案1】:

    您已将数据作为 JSON 发送,而不是普通查询数据,因此它不存在于 request.GET['cnt'] 中。

    最简单的做法是从 JS 中删除 JSON.stringify 调用,因此 ajax 调用的参数就是 data: data

    【讨论】:

    • 好的,这是问题,但知道我在控制台中显示结果后还有另一个问题系统停止
    • 我的意思是在控制台中显示的结果是 [ ] 表示 它是空的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多