简体   繁体   中英

Can't Get AJAX to django

My HTML is like this:

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

My django url is this one:

   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?

I'm getting 404 Not found error

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

Just needed to do like Jinja Templating.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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