1

$.get以下の例のように、jQueryを使用して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配列に表示されない理由を誰かが知っていますか?

編集II

エラーを再現する偽のデータを含むURLを見つけました。

以下の行だけでtest_get.phpというPHPファイルを作成しました。

<?php var_dump($_GET); ?>

そして、以下の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á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"
}

編集III

コメントで示唆されているように、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);

IVを編集

次のURLは機能しますが、電子メールの最後の部分「@foobar.foo」を追加すると失敗します。また、電子メールなしで、他のデータを使用して試行しましたが、失敗します。

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"
}
4

1 に答える 1

2

サーバーでURLをテストしましたが、正常に機能しました。

array
  'cmd' => string 'new_suggestion' (length=14)
  'suggestion' => string 'some suggestion' (length=15)
  'sender' => string '{"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@fo'... (length=882)

サーバーはutf-8をサポートするように構成されていますか?phpinfo()を実行して、何が得られるかを確認します。私は次のようになります:

HTTP_ACCEPT_CHARSET     ISO-8859-1,utf-8;q=0.7,*;q=0.3

URLに奇妙な文字(Romario、Christaeoなど)が含まれていることに気付きました。おそらくそれはutf-8がない場合の問題ですか?

http_accept_charsetを変更する必要がある場合、これが役立つ場合があります:http: //rackerhacker.com/2007/11/15/change-the-default-apache-character-set/

===編集===

サーバーは$_GETの512バイトの制限に達していたことが判明しました。現在、これはWebサーバーごとに異なります。そのため、サーバーで試したときに正常に機能しました。

サーバーをアップグレードする以外の唯一の方法は、.get()の代わりにjquery .post()を使用することでした。

于 2013-01-09T22:06:40.607 に答える