我正在使用 jQuery$.get
将一些数据发送到我的 PHP 服务器,如下例所示:
Javascript:
var usr_data = JSON.stringify(window.fb_user);
var save_data = {
cmd : 'new_suggestion',
suggestion : $('#suggestion').val(),
sender : usr_data
};
$.get('/class/suggestion.php',
save_data,
function( data ) {
if (data.result){
alert('Thanks for your suggestion.');
} else {
alert('Error');
}
});
PHP:
var_dump($_GET);
usr_data 变量是一个对象,其中包含我在用户使用其 Facebook 帐户登录时获得的 Facebook 用户数据。
问题是,有时,根据可用的用户数据,sender
变量消失如下:
array(2) {
["cmd"]=>
string(14) "new_suggestion"
["suggestion"]=>
string(15) "Some suggestion"
}
我无法在此处发布真实的发件人数据,因为它是 Facebook 用户数据,而且我找不到使用虚假数据或我的 Facebook 数据重现错误的方法。
编辑
有人知道为什么sender
没有出现在 $_GET 数组上吗?
编辑二
我刚刚找到了一个带有重现错误的虚假数据的 URL。
我用下面的行创建了一个名为 test_get.php 的 PHP 文件。
<?php var_dump($_GET); ?>
并使用以下网址:
http://mysite/test_get.php?cmd=new_suggestion&suggestion=some%20suggestion&sender=%7B%22id%22%3A%221234567890123%22%2C%22name%22%3A%22Foo%20Bar%20Baz%22%2C%22first_name%22%3A%22Foo%22%2C%22middle_name%22%3A%22Bar%22%2C%22last_name%22%3A%22Baz%22%2C%22link%22%3A%22https%3A%2F%2Fwww.facebook.com%2Ffoobarbaztest%22%2C%22username%22%3A%22foobarbaz%22%2C%22birthday%22%3A%2201%2F01%2F1900%22%2C%22hometown%22%3A%7B%22id%22%3A%221234567890%22%2C%22name%22%3A%22Test%20City%22%7D%2C%22inspirational_people%22%3A%5B%7B%22id%22%3A%22111713915507379%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%2C%7B%22id%22%3A%22106184866078691%22%2C%22name%22%3A%22Romário%22%7D%2C%7B%22id%22%3A%22109601749058079%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%5D%2C%22gender%22%3A%22male%22%2C%22religion%22%3A%22Cristão%20-%20Católico%22%2C%22political%22%3A%22Foo%20Party%22%2C%22email%22%3A%22foobarbaz%40foobar.foo%22%2C%22timezone%22%3A-5%2C%22locale%22%3A%22pt_BR%22%2C%22languages%22%3A%5B%7B%22id%22%3A%22108083115891989%22%2C%22name%22%3A%22Português%22%7D%2C%7B%22id%22%3A%22108177092548456%22%2C%22name%22%3A%22Español%22%7D%2C%7B%22id%22%3A%22106059522759137%22%2C%22name%22%3A%22English%22%7D%2C%7B%22id%22%3A%22108224912538348%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22108106272550772%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22450169151702580%22%2C%22name%22%3A%22Portuguese%22%7D%5D%2C%22verified%22%3Atrue%2C%22updated_time%22%3A%222011-11-02T18%3A43%3A36%2B0000%22%7D
结果是一样的:
array(2) {
["cmd"]=>
string(14) "new_suggestion"
["suggestion"]=>
string(15) "some suggestion"
}
编辑三
正如评论所建议的那样,URL 上有重音字符。我对这些字符进行了编码,然后再次尝试,但没有成功。这是正确编码的 URL:
http://mysite/test_get.php?cmd=new_suggestion&suggestion=some%20suggestion&sender=%7B%22id%22%3A%221234567890123%22%2C%22name%22%3A%22Foo%20Bar%20Baz%22%2C%22first_name%22%3A%22Foo%22%2C%22middle_name%22%3A%22Bar%22%2C%22last_name%22%3A%22Baz%22%2C%22link%22%3A%22https%3A%2F%2Fwww.facebook.com%2Ffoobarbaztest%22%2C%22username%22%3A%22foobarbaz%22%2C%22birthday%22%3A%2201%2F01%2F1900%22%2C%22hometown%22%3A%7B%22id%22%3A%221234567890%22%2C%22name%22%3A%22Test%20City%22%7D%2C%22inspirational_people%22%3A%5B%7B%22id%22%3A%22111713915507379%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%2C%7B%22id%22%3A%22106184866078691%22%2C%22name%22%3A%22Rom%C3%A1rio%22%7D%2C%7B%22id%22%3A%22109601749058079%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%5D%2C%22gender%22%3A%22male%22%2C%22religion%22%3A%22Crist%C3%A3o%20-%20Cat%C3%B3lico%22%2C%22political%22%3A%22Foo%20Party%22%2C%22email%22%3A%22foobarbaz%40foobar.foo%22%2C%22timezone%22%3A-5%2C%22locale%22%3A%22pt_BR%22%2C%22languages%22%3A%5B%7B%22id%22%3A%22108083115891989%22%2C%22name%22%3A%22Portugu%C3%AAs%22%7D%2C%7B%22id%22%3A%22108177092548456%22%2C%22name%22%3A%22Espa%C3%B1ol%22%7D%2C%7B%22id%22%3A%22106059522759137%22%2C%22name%22%3A%22English%22%7D%2C%7B%22id%22%3A%22108224912538348%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22108106272550772%22%2C%22name%22%3A%22French%22%7D%2C%7B%22id%22%3A%22450169151702580%22%2C%22name%22%3A%22Portuguese%22%7D%5D%2C%22verified%22%3Atrue%2C%22updated_time%22%3A%222011-11-02T18%3A43%3A36%2B0000%22%7D
有了这个评论,我注意到 jQuery(或我的浏览器)没有对返回的重音字符进行编码JSON.stringify(window.fb_user);
编辑四
以下 URL 有效,但如果我添加电子邮件“@foobar.foo”的最后部分,它会失败。也试过不用e-mail,但是用其他数据,还是不行。
cmd=new_suggestion&suggestion=some%20suggestion&sender=%7B%22id%22%3A%221234567890123%22%2C%22name%22%3A%22Foo%20Bar%20Baz%22%2C%22first_name%22%3A%22Foo%22%2C%22middle_name%22%3A%22Bar%22%2C%22last_name%22%3A%22Baz%22%2C%22link%22%3A%22https%3A%2F%2Fwww.facebook.com%2Ffoobarbaztest%22%2C%22username%22%3A%22foobarbaz%22%2C%22birthday%22%3A%2201%2F01%2F1900%22%2C%22hometown%22%3A%7B%22id%22%3A%221234567890%22%2C%22name%22%3A%22Test%20City%22%7D%2C%22inspirational_people%22%3A%5B%7B%22id%22%3A%22111713915507379%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%2C%7B%22id%22%3A%22106184866078691%22%2C%22name%22%3A%22Romário%22%7D%2C%7B%22id%22%3A%22109601749058079%22%2C%22name%22%3A%22Pope%20Benedict%20XVI%22%7D%5D%2C%22gender%22%3A%22male%22%2C%22religion%22%3A%22Cristão%20-%20Católico%22%2C%22political%22%3A%22Foo%20Party%22%2C%22email%22%3A%22foobarbaz
array(3) {
["cmd"]=>
string(14) "new_suggestion"
["suggestion"]=>
string(15) "some suggestion"
["sender"]=>
string(509) "{"id":"1234567890123","name":"Foo Bar Baz","first_name":"Foo","middle_name":"Bar","last_name":"Baz","link":"https://www.facebook.com/foobarbaztest","username":"foobarbaz","birthday":"01/01/1900","hometown":{"id":"1234567890","name":"Test City"},"inspirational_people":[{"id":"111713915507379","name":"Pope Benedict XVI"},{"id":"106184866078691","name":"Romário"},{"id":"109601749058079","name":"Pope Benedict XVI"}],"gender":"male","religion":"Cristão - Católico","political":"Foo Party","email":"foobarbaz"
}