0

I have an API that returns two numbers as a response. For example if I call this

$.get('/api/get_limit/').always(function(data) { console.log(data.responseText); });

I expect to see something like this in the console

Object {absolute_limit: 389, concurrent_limit: 999}

Which is what I get, except when one of these numbers is Infinity. In that case when I make the same request, the console will show

Object {readyState: 4, responseText: "{"absolute_limit":Infinity,"concurrent_limit":999.0}", status: 200, statusText: "OK"}

I checked and API is working as expected in both cases.

My question is why this happens and is there a way I can fix this without substituting Infinity with something like -1?

EDIT:

Django code for generating API with the help of Django Rest Framework

class QueriesLimit(APIView):
    def get(self, request, format=None):
        limit = request.user.concurrency_limit()
        abs_limit = request.user.available_queries()
        return Response({'concurrent_limit': limit, 'absolute_limit': abs_limit})
zanderle
  • 815
  • 5
  • 15
  • Can you add the API code that generates the JSON? – Taher Rahgooy Aug 17 '15 at 11:33
  • Why you're using `$.get()` when the return value is (should be) JSON? [PHP: How to encode infinity or NaN numbers to JSON?](http://stackoverflow.com/questions/13581843/php-how-to-encode-infinity-or-nan-numbers-to-json) – Andreas Aug 17 '15 at 11:36
  • Do not attempt to encode JSON manually - you should use an available library to encode your dictionaries/arrays to JSON. Not being terribly familiar with python, this looks like what you should be using: https://docs.python.org/2/library/json.html – Adam Jenkins Aug 17 '15 at 12:07
  • @Adam I am not encoding it manually. I pass it Python object and the APIView class will take care of encoding. The problem seems to be in Infinity not being a valid JSON value – zanderle Aug 17 '15 at 12:12
  • @zanderle - if it was encoding it properly, then it would encode `Infinity` as `"Infinity"` because JSON strings need to be double-quoted. So it's obviously not encoding it properly. – Adam Jenkins Aug 17 '15 at 12:32
  • @Adam the problem was elsewhere. See https://docs.python.org/2/library/json.html#infinite-and-nan-number-values – zanderle Aug 18 '15 at 12:30

1 Answers1

1

cause

{
    "absolute_limit": Infinity,
                      ^^^^^^^^
    "concurrent_limit": 999.0
}

is not a json object

jsxqf
  • 557
  • 3
  • 13