我使用这种方法:
NSDictionary *params = @{@"email" : email,
@"password" : pass
};
NSString* HOST_URL = @"http://server.com:8080";
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST_URL]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"/login"
parameters:params];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
NSDictionary *response = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:operation.responseData options:kNilOptions error:nil];
//HERE YOU HAVE A RESPONSE (your server answer)
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error.localizedDescription);
}];
[operation start];
我有回应:
{"Result":"fail","Message":"unexpected end of JSON input","Data":null}
我的要求是通过POST在变量“data”中发送登录名和密码,但是如果我通过字典中的文本发送请求,如何在我的服务器上发送此变量?
答案 0 :(得分:2)
NSDictionary *params = @{@"email" : email,
@"password" : pass
};
NSString* HOST_URL = @"http://server.com:8080";
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
// Enter what happens here if successsful.
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
// Enter what happens here if failure happens
}
答案 1 :(得分:0)
我明白了:
NSString *URLString = @"http://server.com:8080/login";
// login = @"user1@1q2w3e"; // user1@1q2w3e user3@frcity
// password = @"1q2w3e";
NSString* data = @"{\"email\": \"user1@1q2w3e\" ,\"password\": \"1q2w3e\"}";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
NSDictionary *params = @{@"data" : data};
[manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];