2014-10-07 9 views
6

I AFnetworking Bibliothek bin mit Daten auf dem Server schreibenFehler Domain = com.alamofire.error.serialization.response Code = -1011 „Die Anforderung ist fehlgeschlagen. Schlechte Anfrage (400)

Es folgt mein Codedaten posten auf dem Server.

- (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); 
    }]; 
} 

und als Antwort bekam folgende ich Fehler

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>} 

ich nicht in der Lage bin zu verstehen, warum ich die solche Art von Fehler bekam. Was in meinem Code fehlt?

+0

Sie das Problem bekommen haben? Ich stehe vor demselben Problem. Kannst du bitte in meinen Code schauen? http://stackoverflow.com/questions/41713116/errorrequest-failed-bad-request-400-login-via-afnetworking-in-swift – Coder

Antwort

8

Der Fehler sagt alles: Sie haben eine 400 response vom Server, was bedeutet, dass was Sie gesendet haben war entweder nicht richtig formatiert, oder der Server konnte es einfach nicht verstehen.

2

Bitte fügen Sie diese Zeile zu Ihrem Code, Ich hoffe, es hilft Ihnen.

manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 
0

Ich hatte das gleiche Problem erlebt. In meinem Fall ist es nur für das Hochladen von Bildern mit größerer Größe. Unser Server ist auf maximale Größe beschränkt. Ich habe das Bild verkleinert und hochgeladen, und das Problem ist verschwunden.

können Sie die Größe des Bildes mit:

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

    return newImage; 
} 
Verwandte Themen