简体   繁体   English

将json传递给php并获得响应

[英]Passing json to php and getting response

I am new to php/ajax/jquery and am having some problems. 我是php / ajax / jquery的新手,遇到了一些问题。 I am trying to pass json to the php file, run some tasks using the json data and then issue back a response. 我正在尝试将json传递到php文件,使用json数据运行一些任务,然后发出响应。 I am using the facebook api to get log in a user,get there details, traslate details to json, send json toe the server and have the server check if the users id already exists in the database. 我正在使用facebook api来获取用户登录信息,获取详细信息,将详细信息翻译为json,将json发送到服务器并让服务器检查用户ID是否已存在于数据库中。 Here is my javascript/jquery 这是我的javascript / jquery

 function checkExisting() {

FB.api('/me', function(response) {
  console.log('Successful login for: ' + response.id );

    var json = JSON.stringify(response);
    console.log(json);

    $.ajax({
            url: "php.php",
            type: "POST",           
            data: {user: json},
            success: function(msg){

                  if(msg === 1){
                     console.log('It exists ' + response.id ); 
                  } else{
                       console.log('not exists ' + response.id      ); 
                  }
                }
        })



});
}

Here is my php file 这是我的PHP文件

   if(isset($_POST['user']) && !empty($_POST['user'])) {

$c = connect();
$json = $_POST['user'];
$obj = json_decode($json, true);
$user_info = $jsonDecoded['id'];

$sql = mysql_query("SELECT * FROM user WHERE {$_GET["id"]}");
$count = mysql_num_rows($sql);

if($count>0){
echo 1;
} else{
echo 0; 
} 

 close($c);
}

function connect(){
$con=mysqli_connect($host,$user,$pass);

if (mysqli_connect_errno()) {
  echo "Failed to connect to Database: " . mysqli_connect_error();
 }else{
return $con;
}
 }

 function close($c){
mysqli_close($con);
  }

I want it to return either 1 or 0 based on if the users id is already in the table but it just returns a lot of html tags. 我希望它根据用户ID是否已在表中返回1或0,但它只返回很多html标记。 . The json looks like so json看起来像这样

{"id":"904186342276664","email":"ferrylefef@yahoo.co.uk","first_name":"Taak","gender":"male","last_name":"Sheeen","link":"https://www.facebook.com/app_scoped_user_id/904183432276664/","locale":"en_GB","name":"Tadadadn","timezone":1,"updated_time":"2014-06-15T12:52:45+0000","verified":true} 

Fix the query part: 修复查询部分:

$sql = mysql_query("SELECT * FROM user WHERE {$_GET['id']}");

Or another way: 或另一种方式:

$sql = mysql_query("SELECT * FROM user WHERE ". $_GET['id']);

Then it's always better to use dataType in your ajax 那么在ajax中使用dataType总是更好

    $.ajax({
            url: "php.php",
            type: "POST",           
            data: {user: json},
            dataType: "jsonp", // for cross domains or json for same domain
            success: function(msg){

                  if(msg === 1){
                     console.log('It exists ' + response.id ); 
                  } else{
                       console.log('not exists ' + response.id      ); 
                  }
                }
        })



});

Where is $jsonDecoded getting assigned in your PHP? $jsonDecoded在PHP的哪里分配? Looks unassigned to me. 看起来还没分配给我。

I think you meant to say: 我想你的意思是说:

$obj = json_decode($json, true);
$user_info = $obj['id'];

And your SELECT makes no sense. 而且您的SELECT毫无意义。 Your referencing $_GET during a POST. 您在POST期间引用的$_GET Maybe you meant to say: 也许您是想说:

$sql = mysql_query("SELECT * FROM user WHERE id = {$user_info}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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