1

I am trying to send JSON object but in result it is sending a parse error .

{"readyState":4,"responseText":"","status":200,"statusText":"OK"}

here is code

var data = {
  fb_id: response.id,
  email: response.email,
  name: response.name,
  first_name: response.first_name,
  last_name: response.last_name,
  verified: response.verified,
  birthday:response.birthday,
  picture: response.picture.data.url,
  hometown: response.hometown.name,
  gender: response.gender 
};
$.ajax({
  url:'connect.php',
  type: 'POST',
  data: {
    user: data
  },
  dataType: 'JSON',
  success: function(html){
    //page();
    console.log(JSON.stringify(data));
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    if (XMLHttpRequest.readyState == 4) {
      console.log(JSON.stringify(XMLHttpRequest));
    }
  }

});

and the backend is here: The JSON object is sending here

if(isset($_POST['user'])) {
  //convert json object to php associative array
  $data = $_POST['user'];
  $fbid = $data['fb_id'];             // To Get Facebook ID
  $fbfullname = $data['name']; // To Get Facebook full name
  $femail = $data['email'];    // To Get Facebook email ID
  $fbname = $data['first_name'];
  $fblname = $data['last_name'];
  $fbgender = $data['gender'];
  $fverified = $data['verified'];
  $faddress = $data['hometown'];
  $fbirth = $data['birthday'];
  $img = $data['picture'];

}

The object is sending something like that:

{
    "fb_id":"xxxxxxxxxxx99",

    "email":"sxxxxxx@xxx.in",

    "name":"Sagar xxxx",
    ...
}

PS: I am using 1.12.4 version of jquery.min.js

Updated When I tries to send request using this query to connect.php page it returns error in console log. and If i change dataType to "text" or exclude it then it doesn't return any error but then connect.php cannot identify any query posted using ajax request ,i.e., isset($_POST['user']) will not be able to identify any query .

sagar
  • 732
  • 1
  • 6
  • 25

2 Answers2

0

I'm not a PHP expert, but could be the problem is you are sending JSON as a post body and on backend operate with it as urlencoded form. I suppose you need to get a plain json from request, parse it to an array and then process. You can find useful example here Issue reading HTTP request body from a JSON POST in PHP

Also change dataType to "application/json"

Community
  • 1
  • 1
Stan
  • 1,410
  • 10
  • 14
0

In your php script add this code after reading json:

  if(isset($_POST['user']))
  {
 //convert json object to php associative array
  $data = $_POST['user'];
  $fbid = $data['fb_id'];            // To Get Facebook ID
  $fbfullname = $data['name']; // To Get Facebook full name
  $femail = $data['email'];    // To Get Facebook email ID
  $fbname = $data['first_name'];
  $fblname = $data['last_name'];
  $fbgender = $data['gender'];
  $fverified = $data['verified'];
  $faddress = $data['hometown'];
  $fbirth = $data['birthday'];
  $img = $data['picture']; 

Add this code:

  header("Content-Type: application/json", true); 

  /* Return JSON */
  echo json_encode("Success");

  /* Stop Execution */
  exit; 

}

"Success" text is sent in onSuccess(html)

dardan.g
  • 689
  • 7
  • 17