JQuery and long int id

I ran into the following problem:

Our database has objects with identifiers, such as 4040956363970588323. I am writing some jQuery client master to interact with such objects. The client receives basic data about objects through an Ajax request, for example:

$.ajax({ url: "/api/pages/", type: "get", dataType: "json", data: {"id": site_id}, success: function(data){ if (data.success){ for (var pidx in data.pages){ console.log(data.pages[pidx].id); var li = $('<li class="ui-widget-content"></li>'); var idf = $('<input type="hidden" id="pid" value="{0}"/>'.format(data.pages[pidx].id)) var urlf = $('<input type="hidden" id="purl" value="{0}"/>'.format(data.pages[pidx].url)) li.text(data.pages[pidx].title); li.append(idf); li.append(urlf); $("#selectable_pages_assign").append(li); } pages_was = $("#selectable_pages_assign>li"); } else updateTips(data.message); }, error: function(){ updateTips("Internal erro!"); } }) 

So, as you can see, I am sending data such as a JSON object (server code bit):

 return HttpResponse(dumps({ "success": True, "pages": [{"id": page.id, "title": page.title, "url": page.image} for page in Page.objects.filter(site = site)] })) 

According to Firebug , the server sends the correct identifiers in the data, but console.log(..) instead of the correct id (4040956363970588323), the outputs are id 4040956363970588000 .

Why is this happening?

Without access rights, any chance that my master will work correctly :)

+6
javascript jquery floating-point floating-accuracy
source share
2 answers

My guess is that something is wrong with the conversion to JSON. When you write a value, you may have to put quotation marks around it to make sure it is treated as a string.

+9
source share

This seems like an overflow problem.

According to this discussion here, on SO, JavaScript can only handle INT of size 2 ^ 64, which means that max INT is somewhere around

 184467440737100000 

which is much smaller

 4040956363970588323 

EDIT: Sorry, the largest exact integer is 2 ^ 53, but the case is the same.

+3
source share

All Articles