2

I'm using angular to make an AJAX call to my own REST API.

$scope.authenticate = function() {
    $http({
        headers: httpHeaders,
        url: '/something/',
        method: "POST",
        data: { 'userId' : $scope.userId, 'password': $scope.password },
        cache: false
    })
    .success(function(response, status) {
        console.log(response);
        var ourstring = JSON.stringify(response);
        console.log(ourstring);
        $scope.authenticate_data = response;
        $scope.sessionId = response.sessionId;
        $scope.cookie = response.cookie;
        $scope.message = "You have successfully authenticated."
    });
}

Somehow, angular incorrectly parses integers, and in the Chrome inspector's network tab, the call shows the following:

REQUEST TAB

{"sessionId": 115053508782199126, "cookie": "JSESSIONID=E61DD3443AE9E119060A637CF039936B; Path=/webservice"}

PREVIEW TAB (value stored in scope)

{"sessionId":115053508782199120,"cookie":"JSESSIONID=E61DD3443AE9E119060A637CF039936B; Path=/webservice"}

When I wrap the integer as a string in the backend, everything is fine. However, I really want to get understand what is causing this error.

Related link: Angular $resource does not parse correctly an integer response

6
  • 1
    This question is very unclear. Do you have code with output? Commented Oct 7, 2014 at 4:29
  • What Integer are you trying to pass, where is the integer coming from (where and how are you getting it and passing it), and what exactly is the error you are getting? Commented Oct 7, 2014 at 4:33
  • I've added the low level $http.get. I'm not getting an error. The request is actually coming in correctly, but between the request and the automatic parsing by angular, the 'sessionId' is being changing. Commented Oct 7, 2014 at 4:36
  • The session ID is changing - to what? Commented Oct 7, 2014 at 4:37
  • In the raw request it's coming in as 115053508782199126 --> becomes 115053508782199120 Commented Oct 7, 2014 at 4:38

1 Answer 1

4

There is no such thing as a purely integer variable in JavaScript.

Instead, all we have are Numbers, which are stored as IEEE754 floating-point values. And from the ECMAScript standard, we have:

Note that all the positive and negative integers whose magnitude is no greater than 2^53 are representable in the Number type (indeed, the integer 0 has two representations, +0 and −0).

Note that this does not mean that the largest integer representable in JavaScript is 2^53, but rather that once you get larger than this, only some integers can be represented. Once you've exhausted the 53-bit mantissa of the IEEE754 floating point representation, there start to appear gaps between the integers.

Your example session ID 115053508782199126 is rather larger than 2^53, and it turns out the closest integer floating-point value to it is 115053508782199120. So, you need to use strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the very thorough explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.