AfNetworking 2.0 post issue
I recently wanted to use Afnetworking in my ios app. the same page answers me using ASIHTTPREQUEST
. but that's just not with AFNetworking. Here are my efforts.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"user[height]": @"10",
@"user[weight]": @"255"};
[manager POST:@"http://localhost:8888/TestingPost/post.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Now I tried many options like adding serialization to different xml / json / html. but useless.
On the PHP page, I am not doing something fancy, just printing out what is posted on the page. still here.
<?php
// Show all information, defaults to INFO_ALL
header('Content-type: application/JSON');
print_r($_POST[]);
/*
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$password = $_POST['password'];
*/
//echo( "hurray");
?>
Could you please shed some light on it. I want to go from ASIHTTPREQUEST
newest and more supported ones.
Here is the result for the query
Networking_example[1777:41979] Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=0x7f9feae16d60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f9fedb029a0> { URL: http://localhost:8888/TestingPost/post.php } { status code: 500, headers {
Connection = close;
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Thu, 16 Oct 2014 18:08:08 GMT";
Server = Apache;
"X-Powered-By" = "PHP/5.5.10";
} }, NSErrorFailingURLKey=http://localhost:8888/TestingPost/post.php, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: internal server error (500)}
source to share
If your PHP is using $_POST
it means you are fine using requestSerializer
the default AFHTTPRequestSerializer
(i.e. request application/x-www-form-urlencoded
).
But your JSON is not a generation JSON response, so you need to change responseSerializer
to AFHTTPResponseSerializer
.
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
And your PHP page is reporting a 500 error. This is an "Internal Server Error" for which the status code definition says:
The server encountered an unexpected condition that prevented it from fulfilling the request.
This indicates a PHP error. You may have intended:
print_r($_POST);
Generally, when interacting with a web service, it is better to generate a JSON response, e.g.
echo(json_encode($_POST));
Obviously, if you are creating a JSON response, you can also use the default setting [AFJSONResponseSerializer serializer]
instead of changing it to AFHTTPResponseSerializer
.
source to share