简体   繁体   English

如何防止数字字符串数组在Javascript中向上/向下取整?

[英]How to prevent a numeric string Array from being rounded up/down in Javascript?

I'm creating a Facebook game using HTML & Javascript, I'm currently working on rewarding people for inviting others to the game. 我正在使用HTML和Javascript创建Facebook游戏,目前正在努力奖励邀请其他人加入游戏的人。 To do this I run a check whenever a new player joins - I query Facebook's Graph API to get a list of IDs of people who have invited the new player. 为此,只要有新玩家加入,我都会进行一次检查-查询Facebook的Graph API,以获取邀请新玩家的人员ID列表。

My problem is this, this query returns an array of IDs, which is fine, but it seems as though the IDs are coming back as integers, and because Javascript have a limit on integer sizes in an array, some of these IDs are being rounded up or down by 1 number. 我的问题是,此查询返回一个ID数组,这很好,但是似乎这些ID以整数形式返回,并且因为Javascript对数组中的整数大小有限制,因此其中一些ID会四舍五入向上或向下按1个数字。 This means that some of the IDs are incorrect. 这意味着某些ID不正确。

I'm sending this array to my database using an Ajax Post, but the php file is failing because the rounded IDs don't exist in my database. 我正在使用Ajax Post将此数组发送到我的数据库,但是php文件失败,因为四舍五入的ID在我的数据库中不存在。

The ID column in my database is VARCHAR, so I think the php file will work if the data is sent in a string format instead of an int. 我数据库中的ID列是VARCHAR,所以我认为如果数据以字符串格式而不是int格式发送,则php文件将起作用。 And I know that Javascript has no problem storing the correct IDs in an array if it's storing them as a string instead of an int. 而且我知道Javascript将正确的ID存储为字符串而不是int时,在数组中存储正确的ID没有问题。

My question is, how do I get the response from the Graph API call to be stored as a string, rather than an int? 我的问题是,如何从Graph API调用中获取响应,并将其存储为字符串而不是int? Facebook's documents here: https://developers.facebook.com/docs/graph-api/reference/user/apprequests/ state that when you read the apprequests for a user you receive a list of AppRequest nodes. Facebook的文档位于: https : //developers.facebook.com/docs/graph-api/reference/user/apprequests/ ,当您读取用户的申请时,会收到一个AppRequest节点列表。 This page (on AppRequest nodes) https://developers.facebook.com/docs/graph-api/reference/app-request/ states that the from parameter is returned as a User object. 此页面(在AppRequest节点上) https://developers.facebook.com/docs/graph-api/reference/app-request/声明from参数作为User对象返回。 And this page (on the User object) https://developers.facebook.com/docs/graph-api/reference/user/ states that IDs are returned as a "numeric string". 此页面(在User对象上) https://developers.facebook.com/docs/graph-api/reference/user/声明ID作为“数字字符串”返回。 But the rounding up/down is still happening in Javascript. 但是四舍五入仍然在Javascript中发生。

This is the query to the Graph API: 这是对Graph API的查询:

function getRequests() {

FB.login(function(response) {
  if (response.authResponse) {
    console.log('Welcome!  Fetching your information.... ');

//This is the call to Facebook...
     FB.api('me/apprequests?fields=to,from,id', function(response) {

     if (response.error) {
       console.log('Error - ' + response.error.message);
     }
     else {

    var requestid = [];

//Iterating through the response, and inserting all "from" IDs into the array...
    for(var i = 0; i < response.data.length; i++){
    requestid.push(response.data[i].from.id);
    }

    console.log(requestid);

//Sending the array to the database...
       $.ajax({
        url: 'php_scripts/sendinviterewards.php',
        data: {'requestid' : requestid},
        type: "POST",
        success: function(response){
            console.log(response);
                    }
       });
     }
    });
  } else {
    console.log('User cancelled login or did not fully authorize.');
  }
 }, {scope: 'email, user_birthday'});
}

Perhaps the IDs are being returned as numeric strings, but my use of Javascript is causing the larger ID numbers to be rounded. 可能会将ID作为数字字符串返回,但是我对Javascript的使用导致将较大的ID数字四舍五入。 If this is the case, what would be the best way to solve this problem? 如果是这样,解决此问题的最佳方法是什么? Would I need to use a different language? 我需要使用其他语言吗? Or is there some work around using Javascript? 还是有一些围绕使用Java的工作? Thanks in advance! 提前致谢!

I am not completely sure what is causing this but can you try this : 我不确定是什么原因造成的,但是您可以尝试以下方法:

for(var i = 0; i < response.data.length; i++){
    var id = new String(response.data[i].from.id);  
    requestid.push(id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找数组Javascript中向上和向下舍入的所有浮点值的组合 - Find combination of all float values rounded up and down in an array Javascript 如何防止在javascript中四舍五入大十进制数 - How to prevent large decimal numbers being rounded off in javascript 如何防止添加到Javascript数组的元素被覆盖? - How to prevent elements being added to a Javascript array from being overridden? 防止反斜杠被javascript解析为字符串 - Prevent backslash from being parsed by javascript for a string 如何从JavaScript中的字符串获取数值? - How to get a numeric value from a string in javascript? 如何防止多个“,”被附加在字符串中 - How to prevent multiple “, ” from being appended in the string 如何防止 javascript object 被转换为长度为 1 的对象数组? - How do you prevent a javascript object from being converted into a length 1 array of objects? 如何使用Jquery将此javascript向上/向下数字输入框设置? - How can I make this javascript Up/Down Numeric input box using Jquery? 如何防止在Javascript中创建额外的数组元素? - How can I prevent extra array elements being created in Javascript? 如何在JavaScript中将本地存储的逗号分隔字符串转换为数值数组 - How to convert comma separated string of local storage into numeric array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM