angular.js解析整数错误 [英] angular.js parsing integer incorrectly
问题描述
我使用的角度进行一个AJAX调用我自己的REST API。
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."
});
}
不知何故,角度不正确地分析整数,并在Chrome督察的网络选项卡,呼叫显示以下内容:
Somehow, angular incorrectly parses integers, and in the Chrome inspector's network tab, the call shows the following:
请求TAB
{的sessionId:115053508782199126,曲奇:JSESSIONID = E61DD3443AE9E119060A637CF039936B;路径= / web服务}
preVIEW TAB(存储在范围内的值)
{的sessionId:115053508782199120,曲奇:JSESSIONID = E61DD3443AE9E119060A637CF039936B;路径= / web服务}
当我包裹整数作为在后端的字符串,一切都很好。但是,我真的很想知道是什么原因造成这个错误。
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.
相关链接:<一个href=\"http://stackoverflow.com/questions/22696395/angular-resource-does-not-parse-correctly-an-integer-response\">Angular $资源不能正确分析
推荐答案
相反,我们所拥有的编号
s,它被存储为的 IEEE754浮点值。而从 ECMAScript标准,我们有:
There is no such thing as a purely integer variable in JavaScript.
Instead, all we have are Number
s, which are stored as IEEE754 floating-point values. And from the ECMAScript standard, we have:
请注意,所有的正和负整数,其量值不大于2 ^ 53是在数字型再presentable(的确,整数0具有两个重presentations,+ 0和-0)
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).
请注意,这样做的不的意思是在JavaScript中重新presentable的最大整数是2 ^ 53,而一旦你得到比这个更大的,只有一些整数可以重新presented。一旦你已经用尽了IEEE754浮点重新presentation的53位尾数,也开始出现在整数之间的差距。
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.
您例如会话ID 115053508782199126
是相当大于 2 ^ 53
,和原来的最接近的整数浮点 - 点值是 115053508782199120
。所以,你需要使用字符串。
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.
这篇关于angular.js解析整数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!