1

EDIT: I just added (1) to the initWithBaseURL method and it worked... But if someone can help me understand why this happened would me great :)

(1) self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

This is how I'm echoing in my php script... Maybe you guys can teach me the correct way to output this so I won't have to make patch in my app code (solve the problem in the server side)

$resultado = array('Digitos' => $fila['digitos'], 'Marca' => $fila['marca']);
echo json_encode($resultado);

END OF THE EDIT SECTION...

My problem is that I can't seem to connect correctly to the server. I'm trying to connect to a PHP script using a POST request with a parameter called "user_id". I have subclassed AFHTTPSessionManager to have a different layer handle network/database access (the idea is to make use of several scripts like this one, that's the purpose of this class).

This is the attempt to make the POST request inside the subclass called TarjetaHTTPClient (in success/failure I just send the appropriate message to the delegate waiting for the information):

[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];
    }
}];

This is how I create the manager:

+ (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;
}

This is the error I'm having right now:

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}

I have this test with JQuery of the server script in a simple .html and it works perfectly:

$.post('getTarjetas.php', {'user_id': $('#buscar').val()}, function(data) {
        alert(data);
});

with output:

{"Digitos":"1234","Marca":"VISA"}

Help me... Pretty please? :D

Fdo
  • 1,053
  • 4
  • 15
  • 38
  • possible duplicate of [Request failed: unacceptable content-type: text/html using AFNetworking 2.0](http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-using-afnetworking-2-0) – Paulw11 Mar 26 '14 at 23:47
  • Yeah similar but I was subclassing unlike they did, and wasn't able to fix it with their answer until moments ago. I just edited the first part of my question ^^ – Fdo Mar 27 '14 at 00:01
  • The point of that answer is that the problem is really on your server - as per the error message you are receiving. The `AFJSONResponseSerializer` expects a mime type of application/json, not text/html - you need to change the headers in your php using `header('Content-type: application/json');` – Paulw11 Mar 27 '14 at 00:23

0 Answers0