我有最奇怪的問題,試圖從iOS設備與Django API進行通信,並POST一個NSDictionary,似乎無法使其工作。 好奇的是:相同的代碼就像一個魅力與Ruby API交談一樣。AFNetworking 2.0無法發佈到DJANGO(GET消息收到)
這是我的ObjC方法:
- (void)makePostToServerWithClass:(NSString *)stringClass object:(NSDictionary *)parametersDict success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{ [[UIApplication的sharedApplication] setNetworkActivityIndicatorVisible:YES];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
__block NSError *errorMessage;
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:[baseURLString stringByAppendingString:stringClass] parameters:parametersDict error:&errorMessage];
[request setTimeoutInterval:120];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
success(operation, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", operation.responseString);
NSLog(@"ERROR2: %@", errorMessage.description);
NSLog(@"ERROR3: %@", error.description);
if(operation.responseObject)
errorMessage = [self creteErrorMessageForOperation:operation error:error];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
failure(operation, errorMessage);
}];
[manager.operationQueue addOperation:operation];
}
在該代碼中,URLString將是:
@"http://linuxServerIP:8080/api/v1/create_user/"
和parametersDict將含有的NSDictionary:
(LLDB)PO parametersDict
{ 「raw_password」= 1234; username = testuser; }
在Linux服務器,Django的調試打印:
[28/MAR/2014 13時04分29秒] 「GET/API/V1/create_user/HTTP/1.1」 405 4
而且我得到iOS上的錯誤是:
誤差3:錯誤域= NSCocoaErrorDomain代碼= 3840「的操作 無法完成。 (可可錯誤3840.)「(JSON文本沒有啓動 與數組或對象和選項允許片段未設置。) UserInfo = 0xb44e5b0 {NSDebugDescription = JSON文本不以 開始數組或對象和選項允許片段不。集, NSUnderlyingError = 0xb44e860 「請求失敗:不允許的方法 (405)」}
最奇怪的是:我送一個POST和Linux服務器打印錯誤作爲一個GET
你們有什麼想法我做錯了嗎? 正如我之前說過的,它在Ruby API上運行良好。
我認爲,如果這段代碼在帶有ruby的服務器中工作,而不是在帶有django的服務器中工作,那麼服務器實現是不好的。 – HCarrasko