gpt4 book ai didi

php - AFNetworking 发布错误

转载 作者:行者123 更新时间:2023-11-29 02:49:47 25 4
gpt4 key购买 nike

我正在尝试发送一个 POST 请求,其中有 2 个参数,然后会返回一个 json 对象。我正在使用 AFNetworking,每次都会收到此错误:

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0x16d93980 {com.alamofire.serialization.response.error.response=  <NSHTTPURLResponse: 0x16d8f0d0> { URL: http://LINK } { status code: 404, headers {
Connection = "Keep-Alive";
"Content-Length" = 285;
"Content-Type" = "text/html; charset=iso-8859-1";
Date = "Fri, 11 Jul 2014 16:43:42 GMT";
"Keep-Alive" = "timeout=5, max=99";
Server = "Apache/2.4.9 (Ubuntu)";
} }, NSErrorFailingURLKey=http://LINK, NSLocalizedDescription=Request failed: not found (404), NSUnderlyingError=0x16d8f880 "Request failed: unacceptable content-type: text/html"}

POST Objective-c 请求

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"LINK"

parameters:@{@"number": storeID, @"udid" : [UIDevice currentDevice].identifierForVendor.UUIDString}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];


NSLog(@"%@ = %@", storeID, [UIDevice currentDevice].identifierForVendor.UUIDString);

用于测试的php脚本

    $var = $_POST["number"];
$udid = $_POST["udid"];

echo '{"match": 1}';

最佳答案

您的 AFHTTPRequestOperationManager 序列化程序需要 json 内容类型。但你的 PHP 同行将其响应为 html。

在 PHP 中尝试一下:

$var = $_POST["number"];
$udid = $_POST["udid"];

header('Content-Type: application/json');
echo '{"match": 1}';

或者,您可以为您的管理器设置其他序列化程序并手动解析响应:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

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

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