没有在JSON IOS中获得成功消息

时间:2016-02-16 07:21:07

标签: php ios json afnetworking-3

我正在创建简单的应用程序。我有一个PHP脚本,接受5个参数'firstname','lastname','email','password'和'key'。

这是我的php文件代码。

   <?php
 define("SERVER", 'localhost');
 define("USER", 'root');
 define("PASSWORD", '');
 define("DB", 'test');
 mysql_connect(SERVER,USER,PASSWORD,DB);
 $con = mysql_select_db(DB);
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $firstname = isset($_POST['firstname']) ? mysql_real_escape_string($_POST['firstname']) : "";
    $lastname = isset($_POST['lastname']) ? mysql_real_escape_string($_POST['lastname']) : "";
    $email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";
    $password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";
    $key = isset($_POST['key']) ? mysql_real_escape_string($_POST['key']): "";


    if(!empty($firstname) && !empty($lastname) && !empty($email) && !empty($password) && !empty($key) ){

        if ($key == "12345") {
            $sql = "INSERT INTO `users` (`firstname`,`lastname`, `email`, `password`) VALUES ('$firstname', '$lastname', '$email', '$password');";
            $qur = mysql_query($sql);
            $json = array("status" => 1, "msg" => "Done User added!");
        }
        else{
            $json = array("status" => 0, "msg" =>"key not match");
        }


    }
    else{
        $json = array("status" => 0, "msg" => "Error adding user!");
    }


}else{
    $json = array("status" => 0, "msg" => "Request method not accepted");
}

@mysql_close($conn);

/* Output header */
    header('Content-type: application/json');
    echo json_encode($json);

?>

这是Advanced Rest Client的屏幕截图。 scrren shot of sucessfully inserted data

一切都很顺利。但是当我尝试使用AFNetworking从ios应用程序发布请求时,我总是得到错误的回复。

这是我的Objective-C代码。

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
    [parameters setValue:@"ramu" forKey:@"firstname"];
    [parameters setValue:@"kaka" forKey:@"lastname"];
    [parameters setValue:@"ramu@gmail.com" forKey:@"email"];
    [parameters setValue:@"11111" forKey:@"password"];
    [parameters setValue:@"12345" forKey:@"key"];


    AFHTTPSessionManager *managertwo = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    managertwo.requestSerializer = [AFJSONRequestSerializer serializer];
    [managertwo.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [managertwo POST:[NSString stringWithFormat:@"http://localhost:80/Testing/jsonpost.php"] parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"success! %@",responseObject);
        NSLog(@"%@",parameters);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error: %@", error);
    }];
}

我正在接受这样的回应

    2016-02-16 12:31:36.783 TestingLocal[1626:110064] success! {
    msg = "Error adding user!";
    status = 0;
}
2016-02-16 12:31:36.783 TestingLocal[1626:110064] {
    email = "ramu@gmail.com";
    firstname = ramu;
    key = 12345;
    lastname = kaka;
    password = 11111;
}

我不明白为什么会发生这种情况。我到处搜索是什么问题,但我什么也没得到。请帮忙。

1 个答案:

答案 0 :(得分:2)

在$ _POST中获取数据的服务器代码中,请尝试使用以下命令:

$postdata = file_get_contents("php://input");
//replace $_POST with $postdata

实际上,php:// input允许您读取原始POST数据。

它是$ HTTP_RAW_POST_DATA的内存密集型替代品,不需要任何特殊的php.ini指令。

php://输入不适用于enctype =“multipart / form-data”。

参考http://infopotato.com/blog/index/post