AFNetworking错误:“(200-299)中的预期状态代码,得到404”

时间:2013-02-23 02:42:06

标签: iphone ios objective-c rest afnetworking

我的代码:

NSURL *url = [NSURL URLWithString:@"http:testurl.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSString *email = @"test@test.com";
NSString *password = @"test";

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        email, @"email",
                        password, @"password",
                        nil];

[httpClient postPath:@"/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:AFJSONParameterEncoding];
    NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];

即使我知道服务器是预期的POST我也尝试过GET和PUT但仍然收到同样的错误。我也检查了100次网址。关于导致错误的原因的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:2)

转到将http:testurl.com/myobject放在浏览器中的网址。 404表示页面或资源不存在。

尝试将斜线移动到baseUrl,即将baseURL移至http://testurl.com/,将postPath移至myobject,请参阅this issue

答案 1 :(得分:1)

您的基本网址格式错误。

更改此内容:"http:testurl.com"

对此:"http://testurl.com"