1

I'm trying to start this operation and its not even getting to the NSLOG. I guess its something with my stringUrl, maybe its not suppose to work like this?

This is my code:

NSString *stringUrl = [[NSString alloc]initWithFormat:@"http://example.com/add_user.php?username=%@&password=%@&country=%@&email=%@",[self.usernameTextField text],[self.passwordTextField text], countrySelected, [self.emailTextField text]];

NSURL *url = [NSURL URLWithString:stringUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *dict = JSON;
    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"item"]);
} failure:nil];

[operation start];

Edit: this is what i got from the failure block:

Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/html" UserInfo=0xa2b4300 {NSLocalizedRecoverySuggestion={"type":"1","item": "User added successfully."} , AFNetworkingOperationFailingURLRequestErrorKey=http://EXAMPLE.com/add_user.php?username=aaasafasfasf&password=aaa&country=Angola&email=aaaasfasfasfasfasf>, NSErrorFailingURLKey=http://EXAMPLE.com/add_user.php?username=aaasafasfasf&password=aaa&country=Angola&email=aaaasfasfasfasfasf, NSLocalizedDescription=Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=}

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rotem S
  • 63
  • 1
  • 6
  • Get value for stringUrl with NSLog and paste in your browser, and see what is the result. – edzio27 Jun 30 '13 at 15:48
  • @edzio27 this is what I get: {"type": "0","item":"Username already exists Email already exists "} and it what I wanted to get but its not even gets to the NSLog – Rotem S Jun 30 '13 at 15:51
  • Could you try to use the failure block? – Valent Richie Jun 30 '13 at 15:57
  • Does the browser change any encoding in the string? Particularly in the email address? – Wain Jun 30 '13 at 16:06
  • @verbumdei Edited and now include the failure block. – Rotem S Jun 30 '13 at 16:10
  • 1
    Well, you need to set the content type returned to be text/json or application/json instead of the current one text/html: http://stackoverflow.com/questions/919584/how-to-change-content-type-in-php. AFNetworking is quite strict with the content type, I believe :) – Valent Richie Jun 30 '13 at 16:14
  • @Wain its not a real email now, I can enter even aaaaaa as an email. – Rotem S Jun 30 '13 at 16:14
  • @verbumdei Change this header('Access-Control-Allow-Origin: *') to this: header('Content-Type: text/json'); ? – Rotem S Jun 30 '13 at 16:19
  • 1
    no need to replace, you can add it below the existing `header('Access-Control-Allow-Origin: *');` line – Valent Richie Jun 30 '13 at 16:23

1 Answers1

4

In order to get into the success block of the AFJSONRequestOperation, you also need to adhere to the correct content type to be sent by the server. Currently your server is returning text/html content type as the response, hence it goes into the failure block.

As the failure block message suggests, change the content type returned to be text/json or application/json since you are using the AFJSONRequestOperation.

For PHP, you can change the content type returned by following the answer in this question: How to change content type in php?

You can just add one additional line below your existing header lines:

header('Content-Type: text/json');

No need to replace existing header lines unless there is a header line to set content type into something else other than text/json or application/json.

Community
  • 1
  • 1
Valent Richie
  • 5,226
  • 1
  • 20
  • 21
  • now I have another problem: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 45.) UserInfo=0xa361a40 {NSDebugDescription=Unescaped control character around character 45.} I think that the NSURL is too long. – Rotem S Jun 30 '13 at 16:59
  • 1
    @RotemShukron you will need to check the JSON returned whether it is a valid JSON. You can try the suggestions found here: http://stackoverflow.com/questions/6966349/json-parse-error – Valent Richie Jul 01 '13 at 00:13