I want to send the JsonResponse data in the demo.html and display in it. what is wrong in my code. please help me to solve it.

 function ComAction() {
     let xhr = new XMLHttpRequest(),
     data = new FormData();
     data.append('csrfmiddlewaretoken', getCookie('csrftoken'));
     brands.forEach(function (brand) {
        data.append('brand', brand);
    });

    xhr.open('POST', 'compare_in/', true);
    xhr.onload = function () {
        if (xhr.status === 200) {
            data = JSON.parse(xhr.responseText);
            console.log(data);
            alert('Data received successfully. Brands are ' + data.brands);
            window.location.assign({% url 'demo' %});
        } else if (xhr.status !== 200) {
            alert('Request failed.');
        }
    };
  xhr.send(data);
}

it's my urls:

urlpatterns = [
path('compare_in/', views.compare, name='compare'),
path('demo/', views.demo, name='demo'),
]

it's my views:

  def compare(request):
     if request.method == 'POST':
        compare_res = request.POST.getlist('brand')
        d3 = {'brand':{'x', 'y'}, 'color':{'black', 'white'}}
        response_data = {
            'brands': d3,
            'redirect_url': 'http://127.0.0.1:8000/demo/'}
     return JsonResponse(response_data)
  return render(request, 'prodcutSearch/compare_in.html')


  def demo(request):
     return render(request, 'prodcutSearch/demo.html')

0 Answers