简体   繁体   中英

AFNetworking API Unable to post to server

I am trying to post to a working registration form in php that was made using usercake, and I am trying to make a post request to the registration file when a button is pressed. However nothing happens. I believe I am wording something wrong. Here is my current code.

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://thissite.co/"]];
    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"http://thissite.co/cake/register.php?"
                                                      parameters:@{@"username":@"testingService", @"displayname":@"testingService", @"password":@"thisismypassword", @"passwordc":@"thisismypassword", @"email":@"testingservice@service.com", @"bio":@"I am this amazing bio", @"skills":@"hackingthissuckerfromaframework", @"interests":@"I like to hack", @"skills_needed":@"php anyone", @"company":@"Noneofyourtesting", @"dob":@"nopeeeee", @"occupation":@"noob"}];

}

Looks like you are not performing the request you created. Try adding this code at the end of your method.

AFHTTPRequestOperation * httpOperation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //error handler
}];
[httpClient enqueueHTTPRequestOperation:httpOperation];

Or there is an easier way

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://thissite.co/"]];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
[httpClient postPath:@"cake/register.php" parameters:@{@"username":@"testingService", ...} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //error handler
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM