I am getting this error using NSURLSession when I try to convert the NSData from the request to an NSDictionary object:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7f83f6ba11b0 {NSDebugDescription=Invalid value around character 0.}
I have logged the JSON that is being sent from my PHP backend, and it looks like this:
[{"comment":"refresh2","something":"Bob"},{"comment":"refresh","something":"Bob"},{"comment":"show","something":"Bob"},{"comment":"huh","something":"Bob"},{"comment":"erase replace","something":"Bob"},{"comment":"will it add this one?","something":"Bob"},{"comment":"a comment to add","something":"Bob"},{"comment":"upload","something":"Bob"},{"comment":"work","something":"Bob"},{"comment":"add","something":"Bob"},{"comment":"will it work?","something":"Bob"},{"comment":"did this add?","something":"Bob"},{"comment":"another comment","something":"Bob"},{"comment":"storycomment","something":"Bob"}]
I checked on jsonlint to make sure it is valid JSON...and it is.
Here is the code that produces the error:
NSError *jsonError;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonError];
I am quite baffled as I know that the JSON is valid. One thing I have done in the past to resolve this error was commenting out all of my echo
and print/print_r
statements in the php file that the request is being sent to. However, there are no echo
or print
statements in the php file...and I am still getting this error - any help would be greatly appreciated.
UPDATE
I followed the instructions found here to look for unwanted characters...although I don't know what to look for in the output...if someone could looks this over and tell me if their are any characters involved that would cause the error I am getting that would be awesome. I had to cut the output short - it all won't fit in this question.
2015-01-27 14:14:47.249 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.249 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.250 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.250 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=/ hex=2f
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=
hex=a
2015-01-27 14:14:47.252 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.252 spiritswitch[813:15915] char=f hex=66
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.254 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=z hex=7a
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.255 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.258 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.259 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=x hex=78
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=u hex=75
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=- hex=2d
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.263 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=x hex=78
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=- hex=2d
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.265 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.265 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.266 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.268 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.269 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=p hex=70
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.272 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.274 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.274 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.274 spiritswitch[813:15915] char=0 hex=30
2015-01-27 14:14:47.341 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.342 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=p hex=70
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.345 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.345 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.345 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.347 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.347 spiritswitch[813:15915] char=
hex=a
UPDATE
Here is the PHP
code that handles the response when I make a request - I have been told that I should change the Content-Type
to application/json
but I have tried that and it didn't help - I will try again - but let me know if you see something wrong with this code:
function sendResponse($status=null, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
print $body;
}