如何通过POST和AFNetworking发送变量数据? [英] How to sent data in variable via POST and AFNetworking?

查看:89
本文介绍了如何通过POST和AFNetworking发送变量数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这种方法:

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在变量数据"中发送登录名和密码,但是如果我通过此处的字典中的文本发送请求,如何在服务器上发送此变量?

My requirements is to send login and password via POST in variable "data", but how can I sent this variable on my server if I send request via text in dictionary here?

推荐答案

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


}

这篇关于如何通过POST和AFNetworking发送变量数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆