Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to deal with very large Long numbers in Ajax?

Javascript represents all numbers as double-precision floating-point. This means it loses precision when dealing with numbers at the very highest end of the 64 bit Java Long datatype -- anything after 17 digits. For example, the number:

714341252076979033

... becomes:

714341252076979100

My database uses long IDs and some happen to be in the danger zone. I could change the offending values in the database, but that'd be difficult in my application. Instead, right now I rather laboriously ensure the server encodes Long IDs as Strings in all ajax responses.

However, I'd prefer to deal with this in the Javascript. My question: is there a best practice for coercing JSON parsing to treat a number as a string?

like image 970
Dean Moses Avatar asked Apr 02 '11 20:04

Dean Moses


People also ask

How do I reduce the number of Ajax requests?

This example shows one technique to reduce the number of Ajax calls that are made to the server by caching more data than is needed for each draw. This is done by intercepting the Ajax call and routing it through a data cache control; using the data from the cache if available, and making the Ajax request if not.

How can I improve my Ajax performance?

Reducing the number of requests performed, and the amount of data included in both sides of the transaction, will have a dramatic impact on your Ajax performance. Making server changes will help, too, but may be beyond your control.

When working with Ajax which is faster?

They are all equally fast, the only question is which you find most readable. If you will be making numerous similar ajax calls then it is best to use $. ajaxSetup() in an accessible place (read: near top). This allows you to pre-set your most common settings, thereby creating your own short-cut function.

Are Ajax calls slow?

In my app, there are a lot of ajax calls. A user may do 50 ajax calls per session, so speed is very important. The ajax call queries the database and returns a list of records.


1 Answers

You do have to send your values as strings (i.e. enclosed in quotes) to ensure that Javascript will treat them as strings instead of numbers.

There's no way I know of to get around that.

like image 189
Alnitak Avatar answered Sep 16 '22 18:09

Alnitak