0

我正在尝试将用户发布到支持 rails 的服务器,但我遇到了一个奇怪的错误。当我按下提交时,我在日志中收到 404 错误,并且读出的内容表明该页面不存在。像这样:

Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0xc03c140 {NSLocalizedRecoverySuggestion=<!DOCTYPE html>
<html>
<head>
  <title>The page you were looking for doesn't exist (404)</title>
  <style type="text/css">
    body { background-color: #fff; co...

下面,在日志中打印失败的 url

AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xc033300> { URL: http://example.herokuapp.com/users }, 

所以日志认为这个页面不存在,但是如果我复制并粘贴 url - 页面确实存在,并且我已经能够从 webview 发布到它。出于某种原因,afnetworking 或 nsmutbleURLrequest 声称该页面不存在,当它确实存在时......我的意思是我不想说计算机出错了,但是这里发生了什么?我已经清理了 xcode 并退出了,但没有任何类似的东西可以解决这个问题。有没有人发生过这种情况?

这是我用来发出请求的代码:

+ (void)createUserWithUsername:(NSString *)signature
                         email:(NSString *)email
                      password:(NSString *)password
                         block:(void (^)(User *user))block
{
    NSDictionary *parameters = @{@"signature": signature,
                                 @"email": email,
                                 @"password": password
                                 };

    [[APIClient sharedClient] postPath:@"/users" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        User *user = [[User alloc] initWithDictionary:responseObject];

        if (block) {
            block(user);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        if (block) {
            block(nil);
        }
    }];
}

这就是我从网络端发布的成功帖子中 json 的样子

{"created_at":"2013-07-18T04:05:36Z","email":"grant@gmail.com","id":1,"signature":grant,"updated_at":"2013-07-18T04:05:36Z"}

因此,在使用 Charles 比较成功与失败的帖子请求后,我看到失败的帖子出现 406 错误。此错误可能是正确的,因为成功和失败的帖子都具有相同的帖子路径。所以我不认为它像之前所说的那样是404。但是406?在失败请求的日志中,没有任何关于其内容类型为 json 的问题的参考。这是控制台日志

headers {
    "Cache-Control" = "no-cache";
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 31 Jul 2013 17:24:53 GMT";
    "Proxy-Connection" = "Keep-alive";
    Server = "thin 1.5.1 codename Straight Razor";
    "Set-Cookie" = "_gold_session=BAh7Basdfasdk; path=/; HttpOnly";
    "Transfer-Encoding" = Identity;
    "X-Rack-Cache" = "invalidate, pass";
    "X-Request-Id" = c9c17573dde8b4767ba519aed6e04959;
    "X-Runtime" = "0.008262";
    "X-Ua-Compatible" = "IE=Edge,chrome=1";
} }}
2013-07-31 10:24:57.828 Gold[57543:a0b] User: (null)

这是来自 Charles 的服务器日志的比较,显示了失败的发布请求和成功的发布请求。

这是来自 iphone 的失败帖子的请求

    POST /users HTTP/1.1
Host    mysterious-5550.herokuapp.com
Content-Type    application/json; charset=utf-8
Accept-Encoding gzip, deflate
Content-Length  40
Accept-Language en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5
Accept  application/json
Connection  keep-alive
User-Agent  Gold/1.0 (iPhone Simulator; iOS 7.0; Scale/2.00)

这是失败的响应:

HTTP/1.1 406 Not Acceptable
Cache-Control   no-cache
Content-Type    application/json; charset=utf-8
Date    Wed, 31 Jul 2013 18:26:12 GMT
Server  thin 1.5.1 codename Straight Razor
Set-Cookie  _gold_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFVEkiJTU2OGY2MWZhMzdmNzY2OGRjZGU0ZmQ1Y2Q5YmZiNzAxBjsAVA%3D%3D--3ab7521533c9e8f0f5b4a4d5da4d9f294bdeaba5; path=/; HttpOnly
X-Rack-Cache    invalidate, pass
X-Request-Id    900d86dd0719d0a00eacf803e90fa780
X-Runtime   0.007971
X-Ua-Compatible IE=Edge,chrome=1
transfer-encoding   chunked
Connection  keep-alive

这是来自网络的成功帖子的请求

POST /users HTTP/1.1
Host    mysterious-5550.herokuapp.com
Content-Length  217
Cache-Control   max-age=0
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin  http://mysterious-5550.herokuapp.com
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1580.0 Safari/537.36
Content-Type    application/x-www-form-urlencoded
Referer http://mysterious-5550.herokuapp.com/users/sign_up
Accept-Encoding gzip,deflate,sdch
Accept-Language en-US,en;q=0.8
Cookie  _gold_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJTQ5MGY5MzMzMmNlMzkxZDI4NTllNDU0ZGQ0ZTE5MDg2BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMStZRTNwbGRKQ1VJWXY4aXZtbUMybGt3ejFLcnBZSU5zY053WEcwa2kydDg9BjsARg%3D%3D--2dd8e808965ccc2da457abc94c58c39d080f62ab

这是来自网络的成功帖子的回复

HTTP/1.1 302 Moved Temporarily
Cache-Control   no-cache
Content-Type    text/html; charset=utf-8
Date    Wed, 31 Jul 2013 18:30:11 GMT
Location    http://mysterious-5550.herokuapp.com/
Server  thin 1.5.1 codename Straight Razor
Set-Cookie  _gold_session=BAh7CUkiD3NlFlvdSBoY88c959; path=/; HttpOnly
X-Rack-Cache    invalidate, pass
X-Request-Id    c94b3d921dfe451c562c171524b78b3f
X-Runtime   0.750712
X-Ua-Compatible IE=Edge,chrome=1
transfer-encoding   chunked
Connection  keep-alive

尽管内容类型不同(text/html v json),但我认为这不是问题所在。这两种类型都应该被接受——通常 xcode 会说意外的内容类型。您能看到其他可能导致 406 错误的不兼容内容吗?

4

2 回答 2

0

This code looks fine.

First, go into the postPath:parameters:success:failure: method and set a breakpoint before the last line. Type po request in the console and make sure the URL matches what you expect.

If the URL isn't what you expect, check your initWithBaseURL call and make sure you want that leading slash before "users".

If the URL is what you expect, you need to debug with your server administrator because the server likely needs some other info like authentication or Accept headers. Note that 404 is not the correct status code to return in this case, so you may want to open a discussion about when you use which status codes.

https://github.com/AFNetworking/AFHTTPRequestOperationLogger will help a lot with this - it can log your requests and what they return to the console.

于 2013-07-31T15:49:59.223 回答
-2

服务器应用程序可能需要更多信息,例如 cookie 并返回 404。

于 2013-07-31T00:43:38.227 回答