user1190132 user1190132 - 18 days ago 11
Ajax Question

JSON response from Jquery.ajax is rounding larger number so I can't get correct value

I am performing an AJAX get request and this is the response I get in a program like Paw/Postman:

[
{
"registrantKey": 4131401026087862797,
}
]


However when I do:

.done(function(data, textStatus, jqXHR) {
$.each(data, function(index){
console.log(data[index].registrantKey);
});
})


it is treating this registrant key value as a large number and is rounding off the last numbers so the value being printed to the console is: 4131401026087863000. How do I get the original value as shown in Paw/Postman. I don't need to perform any math operations on it I just need the value. I tried experimenting with the toString() method but I couldn't get this to work. Your help would be much appreciated.

update
Unfortunately this just isn't possible in javascript. The only solution is to use another language or to try and get original developers to return value as a string. Thanks Rory and Madalin.

Answer

If you can access to your json as string and not as object (maybe in the jqXHR object), you can transform all numbers to string like this :

jsonText = jsonText.replace(/([^\D]\d+[^\D])/g, "\"$1\"").replace(/""/g, "\"");

(Be careful with my regex, i don't really sure if it works in any case)

And after load your jsonText as an object.

Comments