Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何通过POST和AFN网络发送变量中的数据?_Ios_Json_Afnetworking_Afnetworking 2 - Fatal编程技术网

Ios 如何通过POST和AFN网络发送变量中的数据?

Ios 如何通过POST和AFN网络发送变量中的数据?,ios,json,afnetworking,afnetworking-2,Ios,Json,Afnetworking,Afnetworking 2,我使用这种方法: NSDictionary *params = @{@"email" : email, @"password" : pass }; NSString* HOST_URL = @"http://server.com:8080"; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWi

我使用这种方法:

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

我得到了这个:

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


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

您正在上载到哪个服务器?如果您需要以普通web表单的方式发送帖子,请查看您是否决定要使用AFNetworking 2.0,如,那么我建议您接受下面user3200708的回答(这样他就可以获得帮助您的“声誉”,您将结束此问题)。你真的不应该同时有这么多(我算是四五个)重复的问题。我需要用变量数据发送数据,你对此感到抱歉。谢谢你清理了你的旧问题!我将在上回答您的“可变数据”问题。在对多个问题的评论中进行讨论毫无意义!