简体   繁体   English

无法将AJAX转换为Django

[英]Can't Get AJAX to django

My HTML is like this: 我的HTML是这样的:

$.ajax({
    type: "GET",
    url:"/ajax/validate_supplier/",
    data:{
        'supplier_name': supplier_name
    },
    dataType: 'json'
});

My django url is this one: 我的django网址是这个:

   path('ajax/validate_supplier/', views.validate_supplier, name='validate_supplier'),

And my view is this: 我的看法是这样的:

def validate_supplier(request):
    supplier_name= request.GET.get('supplier_name',none)
    data={
        'name_is': supplier_name
    }
    return JsonResponse(data)

This simple ajax get is not working for me, what am I doing wrong? 这个简单的ajax get对我不起作用,我在做什么错?

I'm getting 404 Not found error 我收到404找不到错误

in the url give:- 在网址中输入:-

{{url 'appname:validate_supplier'}}

the problem with yours is that it will try to go to the url you have given, and it will show error saying that the url does not exist. 您的问题是它将尝试转到您提供的网址,并且显示错误消息,指出该网址不存在。 you can generally press f12 in chrome to see errors 您通常可以在Chrome中按f12键以查看错误

Just needed to do like Jinja Templating. 只需要做像Jinja模板。

$.ajax({
        type: "POST",
        url:"{% url 'validate_supplier' %}",
        data:{
            'name': supplier_name,
            'phone_number':supplier_phone,
            'email':supplier_email
        },
        dataType: 'json'
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM