簡體   English   中英

錯誤域= com.alamofire.error.serialization.response代碼= -1011“請求失敗:錯誤請求(400)

[英]Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)

我正在使用AFnetworking庫在服務器上發布數據。

以下是我在服務器上發布數據的代碼。

- (void) callLoginAPI:(NSDictionary *)dictProfile{
    // 1
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[dictProfile valueForKey:@"name"], @"username",
                                                                    [dictProfile valueForKey:@"first_name"],@"first_name",
                                                                    [dictProfile valueForKey:@"last_name"],@"last_name",
                                                                    [dictProfile valueForKey:@"email"],@"email",
                                                                    [dictProfile valueForKey:@"birthday"],@"dob",
                                                                    [dictProfile valueForKey:@"gender"],@"gender",
                                                                    [[dictProfile valueForKey:@"location"] valueForKey:@"name"],@"location",
                                                                    [dictProfile valueForKey:@"timezone"],@"timezone",
                                                                    @"",@"language",
                                                                    [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large",[dictProfile valueForKey:@"id"]],@"profile_pic_url",
                                                                    @"",@"cover_pic_url",nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:@"http://10.1.81.35:8000/api/login/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

並作為回應我得到以下錯誤

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7c87b6f0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7cc220e0> { URL: http://10.1.81.35:8000/api/login/ } { status code: 400, headers {
    Allow = "POST, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Tue, 07 Oct 2014 10:45:08 GMT";
    Server = "WSGIServer/0.1 Python/2.7.6";
    Vary = "Accept, Cookie";
    "X-Frame-Options" = SAMEORIGIN;
} }, NSErrorFailingURLKey=http://10.1.81.35:8000/api/login/, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<7b226465 7461696c 223a2022 4a534f4e 20706172 73652065 72726f72 202d204e 6f204a53 4f4e206f 626a6563 7420636f 756c6420 62652064 65636f64 6564227d>}

我無法理解為什么會出現這種錯誤。 我的代碼中缺少什么?

錯誤說明了一切:您從服務器得到了400響應 ,這意味着您發送的內容未正確格式化,或者服務器無法理解它。

請將此行添加到您的代碼中,希望對您有所幫助。

manager.requestSerializer = [AFHTTPRequestSerializer serializer];

我遇到過同樣的問題。 在我的情況下,它只用於上傳更大尺寸的圖像。 我們的服務器限制了max.upload大小。 我調整了圖像大小並上傳,問題就消失了。

您可以使用以下方法調整圖像大小:

+ (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM