5

Using ajax i request a authenticationID like this: enter image description here


This is wrong because the real HTTP-Transfer is this:

enter image description here

(By the way: response-type is "application/json;charset=UTF-8")

I see a clash between

-1369082024195183657 and 
-1369082024195183600

How to prevent the rounding or is it a bug?

Grim
  • 4,939
  • 8
  • 46
  • 97
  • 2
    I didn't count the digits, because you only provide them as image. But the maximum integer in js is `+/-9007199254740991`, everything lager then that has floating point precision. [What is JavaScript's highest integer value that a Number can go to without losing precision?](http://stackoverflow.com/questions/307179) If you want to have a higher precision you need to use a decimal/bignumber library. – t.niese May 24 '16 at 09:08
  • You can try to use float. It looks big. – Razvan Dumitru May 24 '16 at 09:09
  • But isn't HTTP response always a string? It shouldn't be prone to integer limitations unless jQuery tries too hard and decides to convert the response to a number under the hood. – pawel May 24 '16 at 09:12
  • @pawel if it is send as JSON response, then it is number, because there are no quotes. If it is send as plain text is should stay a string. – t.niese May 24 '16 at 09:14
  • @t.niese but it doesn't look like JSON at all, see the screenshot of "response" tab: there's a single negative number, not a key/value pair wrapped in curly braces. – pawel May 24 '16 at 09:16
  • @pawel JSON does not need _curly braces_ : `JSON.stringify(42);` results in `42`. _curly braces_ are only there if you want to represent an object with properties, so `true`, `false`, `42` and `"string"` are all valid JSON data. – t.niese May 24 '16 at 09:17
  • 2
    This question isn't a duplicate in my opinion. The question is about jquery and ajax treating string data as integer data. The question it's marked as a duplicate of is simply about jacascript's limits. The question here can be solved by managing to send the data as a string. – Chris Lear May 24 '16 at 09:21
  • @ChrisLear Will it help to add the java-code? – Grim May 24 '16 at 09:23
  • 2
    Of course it's not a duplicate. The answer would be: set the `dataType` property in your `$.ajax` method to `text` so jQuery doesn't guess it's JSON and doesn't convert to int. i.e. `$.ajax({ dataType : "text", url : "..." })` – pawel May 24 '16 at 09:23
  • @pawel has an answer looks that right to me – Chris Lear May 24 '16 at 09:24
  • And now a possibly valuable answer to a legitimate question will get buried in the comments section because someone went trigger happy without bothering to read and understand the problem ;) – pawel May 24 '16 at 09:28
  • I'd like to re-open this question, but I don't think I can even vote to do that. Pity. – Chris Lear May 24 '16 at 09:29
  • 1
    Question is now reopened. Go ahead, guys. – Frédéric Hamidi May 24 '16 at 09:37

2 Answers2

3

jQuery tries to parse the HTTP response as integer based on the JSON content-type.

> JSON.parse("-1369082024195183657")
-1369082024195183600

You can override it by telling jQuery you expect a string by setting the dataType property in $.ajax configuration:

$.ajax({ 
   dataType : "text", 
   url : "rest/Registration",
   success : function(data){
       // data should be "-1369082024195183657"
   }
})

I guess you don't need to do any arithmetic operations on an authenticationID token, so you can just keep it as a string.

pawel
  • 30,843
  • 6
  • 50
  • 50
0

Yes it is a bug. The Server returns illegal JSON! Created report: https://github.com/FasterXML/jackson-core/issues/291

Grim
  • 4,939
  • 8
  • 46
  • 97