最近 AFNetworking 遇到了一个奇怪的问题。我有一个 PHP 后端,我正在使用 SLIM 框架。正在发生的事情的简化示例:如果我使用链接 http://xxx.xxx.xx.xx/InstaAPI/hi这应该被称为:
$app->get('/hi', function() use($app) {
$app->response->setStatus(200);
echo "hiiii\n";
});
现在在我的 objective-c 代码中我有:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://xxx.xxx.xx.xx/InstaAPI/hi" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"ok");
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSLog(@"fail");
NSLog(@"%@", operation.responseString);
}];
我在输出控制台中得到的结果是:
015-10-08 18:30:20.650 iReporter[12822:3214201] fail
2015-10-08 18:30:20.650 iReporter[12822:3214201] hiiii
不知道它为什么调用失败 block 。状态毕竟设置为200所以应该没问题。有人可以给我一些建议,指出我在这里可能做错了什么吗?
最佳答案
我自己来回答这个问题。结果很简单。我的 header 设置为 'Content-Type', 'application/json'
。 echo "hiiii\n";
这行不是 JSON,所以我不得不使用:
$response = array("Response"=>"HI!");
$echo json_encode($response);
我之所以会遇到这样的错误,是因为我试图通过在愚蠢的地方散布 echo 来调试代码,以检查我的 PHP 脚本中的执行位置。这个故事的寓意是,如果您的 header 设置为 JSON,请不要使用单个回显!
关于php - AFNetworking,AFHTTPRequestOperationManager 为 200 代码时操作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33013375/