从PHP Webservice不返回编码响应correclty

时间:2015-11-17 06:36:31

标签: php ios json xcode afnetworking

我编写了简单的PHP代码来注册我的iphone应用程序中的用户详细信息。它工作正常并返回JSON输出。我在下面添加了代码

header('Content-type: application/json');
include 'connection.php';
$response = array();

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

if($username == NULL || $password == NULL || $email == NULL ){

    $response["success"] = 0;
    $response["message"] = "Something Empty";

}else{

    $sql = "INSERT INTO User (username, password, email)
VALUES ('".$username."', '".$password."', '".$email."')";

if ($conn->query($sql) === TRUE) {

    $response["success"] = 1;
    $response["message"] = "Done";

} else {

    $response["success"] = 0;
    $response["message"] = "Error";
}
}

echo json_encode($response);
$conn->close();

但是当我在添加到表之前尝试检查用户名时已经存在。我从我的Xcode日志中得到错误。

JSON text did not start with array or object and option to allow fragments not set.

if($username == NULL || $password == NULL || $email == NULL ){

    $response["success"] = 0;
    $response["message"] = "Something Empty";

}else{

   $query = mysql_query("SELECT * FROM User WHERE username='".$username."'");


    if (mysql_num_rows($query) != 0)
  {
      $response["success"] = 0;
      $response["message"] = "Username Already Exists";

  }else{

      $response["success"] = 1;
      $response["message"] = "That Name Fine";
  }


}

1 个答案:

答案 0 :(得分:0)

很可能是php错误(可能只是通知或警告,具体取决于您的error_reporting设置)污染了json输出。

您需要调试问题,并且可以考虑error_reporting生产级别。