2014-04-01 18 views
0

我正在使用RESTKit v0.20.3。由於我不期待responseObject,所以我決定使用AFHTTPClient進行POST。我有以下代碼:RESTKit 2.0:帶有文本/ html的AFHTTPClient

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。代碼示例將不勝感激!

回答

1

如果你檢查它實際上是說,HTML的內容:

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

所以,問題不在於你請求的頁面不存在,而不是返回的內容類型是錯誤的。

驗證您正在使用的URL和路徑。