编辑:我刚刚将(1)添加到initWithBaseURL方法并且它有效......但是如果有人能帮助我理解为什么会发生这种情况我会很棒:)
(1) self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
这就是我在我的PHP脚本中的回应...也许你们可以教我正确的输出方式,所以我不必在我的应用程序代码中修补(解决服务器端的问题) )
$resultado = array('Digitos' => $fila['digitos'], 'Marca' => $fila['marca']);
echo json_encode($resultado);
编辑部分结束......
我的问题是我似乎无法正确连接到服务器。我正在尝试使用带有名为“user_id”的参数的POST请求连接到PHP脚本。我已经将 AFHTTPSessionManager 子类化为具有不同的图层处理网络/数据库访问权限(想法是使用像这样的几个脚本,这就是这个类的目的)。
这是尝试在名为 TarjetaHTTPClient 的子类中发出POST请求(成功/失败我只是向代理发送相应的消息等待信息) :
[self POST:@"getTarjetas.php" parameters:@{@"user_id": @"1"} success:^(NSURLSessionDataTask *task, id responseObject) {
if ([self.delegate respondsToSelector:@selector(tarjetaHTTPClient:didUpdateWithTarjetas:)]) {
[self.delegate tarjetaHTTPClient:self didUpdateWithTarjetas:responseObject];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if ([self.delegate respondsToSelector:@selector(tarjetaHTTPClient:didFailWithError:)]) {
[self.delegate tarjetaHTTPClient:self didFailWithError:error];
}
}];
这就是我创建经理的方式:
+ (TarjetaHTTPClient *)sharedTarjetaHTTPClient {
static TarjetaHTTPClient *_sharedTarjetaHTTPClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedTarjetaHTTPClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8888/directoryX/"]];
});
return _sharedTarjetaHTTPClient;
}
- (instancetype)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.requestSerializer = [AFJSONRequestSerializer serializer];
}
return self;
}
这是我现在遇到的错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x109519890 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x1096085b0> { URL: http://localhost:8888/directoryX/getTarjetas.php } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 33;
"Content-Type" = "text/html";
Date = "Wed, 26 Mar 2014 23:35:18 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8y DAV/2 PHP/5.5.3";
"X-Pad" = "avoid browser bug";
"X-Powered-By" = "PHP/5.5.3";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=http://localhost:8888/directoryX/getTarjetas.php}
我在一个简单的.html中使用服务器脚本的JQuery进行了这个测试,它运行得很完美:
$.post('getTarjetas.php', {'user_id': $('#buscar').val()}, function(data) {
alert(data);
});
带输出:
{"Digitos":"1234","Marca":"VISA"}
帮帮我...好吗? :d