i have recently wanted to use Afnetworking in my ios application. the same page responds me by using ASIHTTPREQUEST
. but it simply does not with the AFNetworking. Here is my effort.
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 have tried many options like adding serialization to different xml / json / html. but of no use.
On PHP page i am not doing something fancy just printing out whatever is posted on the page. still here it is.
<?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");
?>
Can you please shed some light on it. i want to switch from ASIhttprequest
to newest and more supported.
Here is the result for the request
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)}
If your PHP is using $_POST
, that means that you are ok using the default requestSerializer
of AFHTTPRequestSerializer
(i.e. an application/x-www-form-urlencoded
request).
But your JSON is not generation JSON response, so you have to change the responseSerializer
to AFHTTPResponseSerializer
.
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
And your PHP page is reporting a 500 error. That is an "Internal Server Error", for which the status code definition says:
The server encountered an unexpected condition which prevented it from fulfilling the request.
That suggests a PHP error. Perhaps you intended:
print_r($_POST);
Generally, when interacting with web service, it's better to generate JSON response, e.g.
echo(json_encode($_POST));
Obviously, if you generate JSON response, you might also want to use the default [AFJSONResponseSerializer serializer]
setting, rather than changing it to AFHTTPResponseSerializer
.