1

最近在使用 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

不知道为什么它调用失败块。状态毕竟设置为200,所以应该没问题。有人可以给我一些指示我在这里可能做错了什么吗?

4

1 回答 1

1

这个我自己来回答。结果很简单。我的标题设置为'Content-Type', 'application/json'. 该行echo "hiiii\n";不是 JSON,所以我不得不使用:

$response = array("Response"=>"HI!");
$echo json_encode($response);

我遇到这样的错误的原因是因为我试图通过在愚蠢的地方分散回声来调试代码,以检查我的 PHP 脚本中的执行位置。故事的寓意,如果您的标头设置为 JSON,请不要使用单个回显!

于 2015-10-08T11:10:01.830 回答