我最近想在我的iOS应用程序中使用AFNetworking。同一页内容使用ASIHTTPRequest
时可以正常响应,但是使用AFNetworking却不能。这是我的尝试。
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);
}];
我已经尝试了很多选项,比如在不同的xml / json / html中添加序列化,但都没有用。
在PHP页面上,我并没有做什么花哨的事情,只是将页面上发布的内容打印出来。但问题仍然存在。
<?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");
?>
请您详细说明一下。我想从ASIhttprequest
切换到更新且更受支持的工具。
这是请求的结果:
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)}
application/json
),但没有返回JSON。要么改成返回JSON,要么更改该头文件(例如application/text
)。 - Rob