1

I'm passing an object of data from a Python/Django App to the front end using AJAX in JSON form. Everything is working correctly, except that I cannot parse the JSON object once in Javascript. I keep getting undefined. I've tried every approach I could think of and I am super stuck so I wanted to see if someone might be able to point out what I hope is a super simple oversight!

Snippet of Python/Django:

data = serializers.serialize('json', products)


  response = {'product_data': data,
              'product_count': product_count}
  return HttpResponse(json.dumps(response), content_type='application/json')

Snippet of AJAX Callback:

.done(function(data){

  console.log(data.product_count)
  console.log(data.product_data)
  console.log(data.product_data["model"])
  console.log(data.product_data[0])
  console.log(data.product_data[0]["model"])

})

Console Logs Response Snippet:

>1
>[{"model": "seller.product", "pk": 11048, "fields": {"seller": 132, "width": 211, "height": 3, "length": 350, "weight": 18600, "price": 11077, "color_id": null, "po_number": null, "po_sub_number": null, "custom_order_id": null, "active_at": "2019-08-02T01:27:23.410Z", "deactive_at": null, "in_holding": false, "approved_at": "2019-08-04T15:34:08.318Z", "sold_at": "2020-02-07T20:07:54.675Z", "slug": "colored-beni-ourain-rug", "created_at": "2019-08-02T01:23:51.650Z", "updated_at": "2020-02-07T20:07:54.675Z", "assets": [3567, 3581, 3585, 3572, 3573, 3574, 3577, 3582, 3583, 3584, 3586, 3587, 3589, 3594, 3596], "colors": [1, 2, 3, 4, 12], "shipping_options": [1]}}]
> undefined
> [
> undefined

The first console log of 1 is correct and in the second line the data I want is all there. But any time I try and get info out of it I just get undefined or a ] and I cannot figure it out. Can anyone help? Thank you!

3 Answers 3

1

Use JSONResponse instead of HttpResponse. That way you don't need to handle anything in between.

return JSONResponse(products)

:)

"json.dumps" would've been sufficient tho, no need for another call to the serializer.

Sign up to request clarification or add additional context in comments.

3 Comments

JsonReponse alone did not work. But added serializer with JsonResponse did. All working now. Thank you!
happy it worked. tho, all, json.dump, serializer and jsonresponse use serialization for objects. so, idk what actually changed for you there :D it seems like you have an object that cant be serialized by the standard serializer and is converted twice. without the actual model its hard to guess what is happening. :)
hey muzzletov, thought you (or anyone else) might want to know, the issue in the model was that there was a QuerySet in the model. I was able to develop a better solution with the use of the following post, which I think was a better question than mine: stackoverflow.com/questions/16790375/…
1

You're serializing you data multiple times, just put products into response without serializing it.

  response = {'product_data': products,
              'product_count': product_count}
  return HttpResponse(json.dumps(response), content_type='application/json')

2 Comments

Thanks for your answer. That's how I had it originally but got the following traceback: TypeError: Object of type ProductQuerySet is not JSON serializable
That's interesting, ProductQuerySet is not JSON serializable but serializers.serialize can serialize it...
0

I believe you need to JSON.parse(data.product_data) first.

1 Comment

Unfortunately, no luck. Parsing is just returning blank arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.