这是我第一次将 JSON 发送到服务器,我不知道为什么我的 PHP 脚本没有收到调用。
我认为问题在于我如何从应用程序中设置 POST 变量,并且我抓取了错误的变量或未按$_POST['search']
预期设置。
谁能指出我将如何获取发布的数据以及如何$_POST['search']
正确设置
当我从 xcode 输出中查看它时,我的$var
回报。0
PHP
header('Content-Type: text/json');
$var = (isset($_POST['search']) ? json_decode($_POST['search']) : false);
echo json_encode($var)
Objective-C
NSDictionary *myJson=@{@"userID": @"1",
@"search":@{@"for":@"routine",
@"page":@"1",
@"orderBy":@"new",
@"type":@"1"}
};
NSURL *url = [[NSURL alloc]initWithString:@"http://192.168.1.64/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
httpClient.parameterEncoding = AFJSONParameterEncoding;
NSDictionary *params = myJson;
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"http://192.168.1.64/igym/bootstrap.php" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"Inside the success block %@",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(@"json text is: %@", JSON);
NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
}];
[operation start];