Cocoa error 3840 using JSON (iOS)

83,780

Solution 1

Unless you pass the option NSJSONReadingAllowFragments to [NSJSONSerialization JSONObjectWithData:options:error:] the response from the server must be valid JSON with a top level container which is an array or dictionary.

for example:

    { "response" : "Success" }

P.S. If you want a mutable dictionary you must also include NSJSONReadingMutableContainers in your options.

Solution 2

It may possible that, the response from your server doesn't contain valid JSON.

Technically, The JSON object must be start with either an "array" or an "object (dictionary)".

So, Whatever your server is returning isn't.

And, you can force the JSON to be consumed regardless by using the NSJSONReadingAllowFragments option.

by using ,

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

Solution 3

You can get this issue if you're connected to VPN on your iOS device.

Share:
83,780
iosdevrocks
Author by

iosdevrocks

Amateur iOS developer :)

Updated on February 01, 2020

Comments

  • iosdevrocks
    iosdevrocks over 4 years

    I'm trying to send data to a server and receive the response in JSON format. The problem is that the server has to return "success" or "fail" but it returns "(null)".

    Here's the returned error:

    Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=XXXXXXXXX {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

    Is it possible that the error is in the server script?

    Here's my function to send the data and receive the response:

    - (void) putData:(NSString *)parameter valor:(NSString *)valor {
    
        NSString *rawString = [NSString stringWithFormat:@"%@=%@", parameter, valor];
        NSData *data = [rawString dataUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:@"http://www.xxx.xxx/xxx.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"PUT"];
        [request setHTTPBody:data];
        NSURLResponse *response;
        NSError *error;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
        NSLog(@"responseData: %@ error: %@", json, error);
    }
    
  • iosdevrocks
    iosdevrocks over 11 years
    What you're saying is that the mistake might be in the PHP code?
  • RickiG
    RickiG over 10 years
    I is likely JSONP, so JSON, but wrapped in a method named something like callback(_all your JSON here);
  • skram
    skram over 9 years
    @RickiG So, If you have no control over the server's response, how would you get by this? By using NSJSONReadingAllowFragments ? If that's the case, does this automatically parse the containing JSON? How would you hanlde it as a response? Thanks!
  • Ashish P
    Ashish P over 9 years
    @Stu i cant solve this issue with or without NSJSONReadingAllowFragments
  • Włodzimierz Woźniak
    Włodzimierz Woźniak over 7 years
    Also you should try AFHTTPResponseSerializer (instead AFJSONResponseSerializer). It works for me
  • Włodzimierz Woźniak
    Włodzimierz Woźniak over 7 years
    Also you should try AFHTTPResponseSerializer (instead AFJSONResponseSerializer). It works for me