【问题标题】:RESTKit 2.0: AFHTTPClient with text/htmlRESTKit 2.0:带有文本/html 的 AFHTTPClient
【发布时间】:2014-04-01 23:54:43
【问题描述】:

我正在使用 RESTKit v0.20.3。由于我不期待 responseObject,我决定使用 AFHTTPClient 进行 POST。我有以下代码:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFJSONParameterEncoding;
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

[httpClient postPath:@"/update" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
   NSLog(@"AFJSONRequestOperation Alt response MIMEType %@",[responseObject MIMEType]);
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"***ERROR*****: %@", [error localizedDescription]);
}];

这是我的日志

2014-04-01 16:43:01.125 App[13181:60b] E restkit.network:RKObjectRequestOperation.m:209 POST 'http://api.domain.com' (200 OK) [0.1158 s]: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html" UserInfo=0xccab6a0 {NSLocalizedRecoverySuggestion=<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Not Found</h1><h2>404</h2><pre>Error: Not Found
    at Object.handle (/var/app/current/app.js:175:15)
    at next (/var/app/current/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at pass (/var/app/current/node_modules/express/lib/router/index.js:110:24)
    at Router._dispatch (/var/app/current/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/var/app/current/node_modules/express/lib/router/index.js:33:10)
    at next (/var/app/current/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at SessionStrategy.module.exports.strategy.pass (/var/app/current/node_modules/passport/lib/middleware/authenticate.js:314:9)
    at SessionStrategy.authenticate (/var/app/current/node_modules/passport/lib/strategies/session.js:61:12)
    at pass (/var/app/current/node_modules/passport/lib/authenticator.js:333:31)
    at deserialized (/var/app/current/node_modules/passport/lib/authenticator.js:345:7)</pre></body></html>, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xda43280> { URL: http://api.domain.com/update }, NSErrorFailingURLKey=http://api.domain.com/update, NSLocalizedDescription=Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xdd50720> { URL: http://api.domain.com/update } { status code: 200, headers {
    "Content-Length" = 1083;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Tue, 01 Apr 2014 23:43:12 GMT";
    "Proxy-Connection" = "Keep-alive";
    Server = "nginx/1.4.3";
    "X-Powered-By" = Express;
} }}
2014-04-01 16:43:01.126 App[13181:60b] ***ERROR*****: Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html

收到text/html 时出现错误,但我如何告诉AFHTTPClient 期待text/html

我尝试添加:

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

返回:

2014-04-01 16:48:13.110 App[13289:60b] ***ERROR*****: The operation couldn’t be completed. (Cocoa error 3840.)

如果我使用AFHTTPRequestOperation,如何传递参数给它?我正在使用 RESTKit,所以我不能使用 AFNetworking 2.0。代码示例将不胜感激!

【问题讨论】:

    标签: ios restkit afnetworking restkit-0.20


    【解决方案1】:

    如果您检查 HTML 的内容,它实际上会说:

    <h1>Not Found</h1><h2>404</h2>
    

    所以,问题不在于您请求的页面不存在,而是返回的内容类型错误。

    验证您使用的 URL 和路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多