简体   繁体   中英

Parsing JSON returning null

I have the following JSON returned from Facebook:

{"id":"123456789","email":"darren@darren.com","first_name":"Darren","gender":"male","last_name":"Sweeney","link":"http://www.facebook.com/123456789","locale":"en_GB","name":"Darren Sweeney","timezone":0,"updated_time":"2014-09-26T22:26:59+0000","verified":true}

I'm trying to ajax it to a PHP file for processing but getting NULL back from the PHP file:

JQuery:

var tmp = JSON.stringify(response);

$.ajax({
      type: 'POST',
      url: '/ajax/fbLogin.php',
      data: {'userData': tmp},
      success: function(data) {
        console.log(data);
      }
});

PHP:

if(isset($_POST['userData'])) {
  $json = $_POST['userData'];
  var_dump(json_decode($json));
} else {
  echo "Error";
}

Any ideas where I'm going wrong?

ANSWER

Posting answer as other users might need this.

As pointed out by @nik in comments, Facebook response does not need processing, so, this now works as it should:

JQuery:

$.ajax({
  type: 'POST',
  url: '/ajax/fbLogin.php',
  data: {'userData': response},
  success: function(data) {
    console.log(data);
  }
});

PHP:

if(isset($_POST['userData'])) {
  $json = $_POST['userData'];
  var_dump($json);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM