0

I am trying to get the data from ajax to django post view, I create the cart with items now I need to get the cart to backend

My js code is:

$.ajax({
  url: url,
  type: "POST",
  data: JSON.stringify({'order_data':order}),
  contentType: 'application/json; charset=UTF-8',
  success: function (data) {
    if (data['success']) {
        alert(data['success']);
    }
  },
  error: function (request, error, status) {
    if (request) {

    }
  }
});

My View code that I am using in Django view:

if request.method == "POST":
  data = json.loads(request.body)
  cart = data['order_data']

Then I am getting that error

 RawPostException: You cannot access body after reading from request's data stream

your suggestions will be preferred

2 Answers2

0

You can get values from POST via:

order_data = request.POST.get('order_data')
D.DOG
  • 48
  • 4
  • order_data is not any input field its the dictionary that I creating from jquery and then send it to back, this POST.get give me none – Mahad_Akbar Dec 01 '20 at 13:53
0

I found the solution for it where I need to do the little change in Django view

 order_data = json.loads(request.POST.get('order_data'))

and my work is done