jquerydjangoajax

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


Solution

  • 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