2015-04-22 90 views
2

我正在使用AFNetworking的Web服務。它在NSURLConnection上工作正常。它使用AFNetworking給出錯誤。錯誤域= AFNetworkingErrorDomain代碼= -1011「請求失敗:方法不允許(405)」

這裏是我的代碼:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL]; 
    manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

    [manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { 
     NSLog(@"Succes"); 
    } failure:^(NSURLSessionDataTask *task, NSError *error) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil]; 
     [alertView show]; 
    }]; 

這是我收到的錯誤:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: method not allowed (405)" UserInfo=0x1e86b390 {NSErrorFailingURLKey=http://..............url.........., AFNetworkingOperationFailingURLResponseErrorKey=, NSLocalizedDescription=Request failed: method not allowed (405)}. 

能否請您爲這方面的幫助。

回答

2

如果您的API是RestFul,那麼您想要使用http請求。使用下面的代碼可能對您有所幫助。請讓我們知道您的意見。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 

NSString *post =[dict JSONRepresentation]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]]; 

[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; 
[request setHTTPBody:postData]; 

//Add your request object to an AFHTTPRequestOperation 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ; 
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSString *response = [operation responseString]; 
    NSLog(@"response: %@",response); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"error: %@", [operation error]); 
}]; 

//call start on your request operation 
[operation start]; 
+1

非常感謝。它工作正常。 –

相關問題