graga
asked on
iOS AFNetworking POST parameters sent from XCode are empty in URL.
Hi Experts,
I am very new into iOS programming and I need your help.
I have a simple code in XCode that uses GET to send data to URL and I want to change this to a POST.
When I test the call, both methods work okay, but running from XCode on simulator, GET works okay but POST returns empty value.
AFHTTPRequestOperationMana ger returns success.
Here is the code for both:
- (IBAction)cmdTestGET:(id)s ender {
AFHTTPRequestOperationMana ger *manager = [[AFHTTPRequestOperationMa nager alloc] init];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:@"http://mytestwebsite/index_test.php"
parameters: @{@"method": @"Test_GET",
@"data": @"Hello GET!"}
success:^(AFHTTPRequestOpe ration *operation, id responseObject) {
NSLog(@"JSON: %@", [responseObject description]);
}
failure:^(AFHTTPRequestOpe ration *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
}];
}
- (IBAction)cmdTestPost:(id) sender {
AFHTTPRequestOperationMana ger *manager = [[AFHTTPRequestOperationMa nager alloc] init];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://mytestwebsite/index_test.php"
parameters: @{@"method": @"Test_POST",
@"data": @"Hello POST!"}
success:^(AFHTTPRequestOpe ration *operation, id responseObject) {
NSLog(@"JSON: %@", [responseObject description]); }
failure:^(AFHTTPRequestOpe ration *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
}];
}
And they returned respectively:
JSON: {
code = 1;
data = OK;
method = "AM_Test_GET";
status = 200;
value = "Hello GET!";
}
JSON: {
code = 0;
data = "<null>";
method = "<null>";
status = 200;
}
the php page that is called is attached
I am very new into iOS programming and I need your help.
I have a simple code in XCode that uses GET to send data to URL and I want to change this to a POST.
When I test the call, both methods work okay, but running from XCode on simulator, GET works okay but POST returns empty value.
AFHTTPRequestOperationMana
Here is the code for both:
- (IBAction)cmdTestGET:(id)s
AFHTTPRequestOperationMana
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer
[manager GET:@"http://mytestwebsite/index_test.php"
parameters: @{@"method": @"Test_GET",
@"data": @"Hello GET!"}
success:^(AFHTTPRequestOpe
NSLog(@"JSON: %@", [responseObject description]);
}
failure:^(AFHTTPRequestOpe
NSLog(@"Error: %@", [error description]);
}];
}
- (IBAction)cmdTestPost:(id)
AFHTTPRequestOperationMana
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer
[manager POST:@"http://mytestwebsite/index_test.php"
parameters: @{@"method": @"Test_POST",
@"data": @"Hello POST!"}
success:^(AFHTTPRequestOpe
NSLog(@"JSON: %@", [responseObject description]); }
failure:^(AFHTTPRequestOpe
NSLog(@"Error: %@", [error description]);
}];
}
And they returned respectively:
JSON: {
code = 1;
data = OK;
method = "AM_Test_GET";
status = 200;
value = "Hello GET!";
}
JSON: {
code = 0;
data = "<null>";
method = "<null>";
status = 200;
}
the php page that is called is attached
I couldn't see your attachment file, please attach it again. I think the problem is from your server side and your above code seems fine. However, if you are learning iOS, I strongly suggest you to work with NSURLSession instead of AFNetworking unless you have a reason. NSURLSession supports block based callbacks, easy to use and less overhead.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have solved the problem as specified in my previous comment