afnetworking post无法发送param

时间:2016-05-11 07:58:44

标签: php ios

我在xcode中的代码:

> NSDictionary *param = @{@"username":
> self.username.text,@"password":self.password.text};
>     NSString *urlString = @"http://dwjdwj.cn/index.php";
>     NSLog(@"%@",param);
>     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
>     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
>     manager.requestSerializer = [AFJSONRequestSerializer serializer];
>     manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json",
> @"text/javascript",@"text/html",nil];
>     [manager POST:urlString parameters:param progress:nil success:^(NSURLSessionTask * _Nonnull task, id  _Nullable
> responseObject) {
>         NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
>         NSLog(@"%@",string);
>     } failure:^(NSURLSessionTask * _Nullable task, NSError * _Nonnull error) {
>         NSLog(@"Error: %@",error);
>     }];

我在服务器中的代码:

> $con = mysql_connect("localhost", "root", "123");     if (!$con)
>   {     die('连接失败: ' . mysql_error());    }   else    {
>       $username=isset($_POST['username'])?$_POST['username']:false;
>       $password=isset($_POST['password'])?$_POST['password']:false;       echo
> "username = ".$username;      echo "password = ".$password;
>       $db_selected = mysql_select_db("app", $con);        $check_query =
> mysql_query("select * from user where username='$username' and
> password='$password' limit 1");       if ($result =
> mysql_fetch_array($check_query)) {            echo "log success";         }else{
>           echo "log failure";         }       mysql_close($con);  }

和我的xcode控制台日志:

HttpDemo[23512:4507807] username = password = log failure

似乎php无法获得帖子参数。我做错了吗?

1 个答案:

答案 0 :(得分:0)

要将请求作为表单发送,请将requestSerializer设置为AFHTTPRequestSerializerAFJSONRequestSerializer将内容类型设置为application/json

如果您确实需要使用JSON进行交换,那么您应该在服务器上阅读:

$json = file_get_contents('some url');
$data = json_decode($json);