2016-05-26 39 views
0

我正在使用HTML创建Facebook游戏& Javascript,我目前正致力于奖励邀请他人参与游戏的人。要做到这一点,我会在新玩家加入时执行检查 - 我查询Facebook的Graph API以获取邀请新玩家的人员ID列表。如何防止在Javascript中向上/向下舍入数字字符串数组?

我的问题是这个,这个查询返回一个ID数组,这很好,但它似乎好像ID作为整数返回,并且因为JavaScript对数组中的整数大小有限制,其中一些ID正在向上或向下舍入1个数字。这意味着一些ID不正确。

我发送这个数组到我的数据库使用Ajax Post,但PHP文件失败,因为我的数据库中不存在舍入的ID。

我的数据库中的ID列是VARCHAR,所以我认为如果数据是以字符串格式而不是int发送的,那么PHP文件就可以工作。而且我知道,如果将正确的ID存储为一个字符串而不是int,那么Javascript将正确的ID存储在数组中并没有问题。

我的问题是,我如何从Graph API调用得到的响应存储为字符串,而不是int? Facebook的文档在这里:https://developers.facebook.com/docs/graph-api/reference/user/apprequests/指出,当您读取用户的apprequests时,您会收到AppRequest节点列表。此页面(在AppRequest节点上)https://developers.facebook.com/docs/graph-api/reference/app-request/指出from参数作为User对象返回。此页面(位于用户对象)https://developers.facebook.com/docs/graph-api/reference/user/指出ID作为“数字字符串”返回。但是,向上/向下舍入仍然在Javascript中发生。

这是查询到的图形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'}); 
} 

或许ID被返回作为数字串,但我使用Javascript是造成四舍五入较大的ID号。如果是这样的话,解决这个问题最好的办法是什么?我需要使用不同的语言吗?还是有一些使用Javascript的工作?提前致谢!

+0

“和JavaScript,因此对数组中的整数大小的限制” ......你有IDS比'数大.MAX_SAFE_INTEGER'(2^53-1)这是9007199254740991 ..? – Redu

+0

嗨@Redu,我有数字大于这个数字,他们不是我数据库中的ID,他们是Facebook分配给玩家的,所以我无法控制他们,但我只是设法解决它 – Emily

+0

好吧,然后转换他们串起来似乎是一种方式。 – Redu

回答

0

我不能完全确定是什么原因造成这一点,但你可以试试这个:

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