6

I've faced with a next problem:

In our database we have objects with ids, like 4040956363970588323. I'm writing some client-wizard on jQuery for interacting with such objects. Client receives base data about objects trough an Ajax request, like:

$.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 see I send data like JSON object (a bit of server code):

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, server send right ids in data, but console.log(..) instead of correct id (4040956363970588323), outputs id 4040956363970588000.

Why does this happen?

Without right ids, any chance, that my wizard will work correctly :)

Jeff Walden
  • 7,008
  • 2
  • 38
  • 55
Anton Koval'
  • 4,863
  • 5
  • 31
  • 44
  • FWIW, your error message says `updateTips("Internal erro!");` (typo on the word "error"). Which is kind of amusing :P – Noon Silk Feb 27 '10 at 13:40

2 Answers2

9

My guess is something is going wrong in the conversion to JSON. When you write the value, you'll probably need to put quotes around it, to make sure it's treated as a string.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • And how do you do that? I'm grabbing a value from HTML5 data and trying to treat it as a string but it's still being rounded incorrectly: `'' + $(this).data('id') + ''`. Is there a better way to do this? – andrewtweber Jun 09 '12 at 02:59
3

That looks like some kind of overflow problem to me.

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

184467440737100000

which is much less than

4040956363970588323

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

Community
  • 1
  • 1
Adrian Schmidt
  • 1,886
  • 22
  • 35
  • 1
    JavaScript numbers are IEEE 754 double-precision floating point values, with the sole qualification that the ECMAScript standard on which JavaScript is based provides only a single `NaN` value rather than perhaps more. Thus, you lose integer precision when when you exceed the precision of the representation, which exactly preserves all integers with magnitude less than 2**53, and only preserves some integers outside that range (ones with nice binary representations, more or less, thinning out as magnitude increases until complete overflow occurs to the infinity values). – Jeff Walden Feb 27 '10 at 15:06