gpt4 book ai didi

php - AfNetworking 2.0 发布问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:52 25 4
gpt4 key购买 nike

我最近想在我的 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)}

最佳答案

如果您的 PHP 使用 $_POST,这意味着您可以使用 AFHTTPRequestSerializer 的默认 requestSerializer(即 应用程序/x-www-form-urlencoded 请求)。

但您的 JSON 不是生成 JSON 响应,因此您必须将 responseSerializer 更改为 AFHTTPResponseSerializer

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

并且您的 PHP 页面报告了 500 错误。这是一个“内部服务器错误”,the status code definition说:

The server encountered an unexpected condition which prevented it from fulfilling the request.

这表明存在 PHP 错误。也许你打算:

print_r($_POST);

一般来说,在与网络服务交互时,最好生成 JSON 响应,例如

echo(json_encode($_POST));

显然,如果您生成 JSON 响应,您可能还想使用默认的 [AFJSONResponseSerializer serializer] 设置,而不是将其更改为 AFHTTPResponseSerializer

关于php - AfNetworking 2.0 发布问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26411222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com