Cocoa错误3840使用JSON(iOS) [英] Cocoa error 3840 using JSON (iOS)
问题描述
我正在尝试将数据发送到服务器,并以JSON格式接收响应。问题是服务器必须返回成功或失败,但它返回(null)。
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)".
这是返回的错误:
错误Domain = NSCocoaErrorDomain Code = 3840操作不能是
完成(Cocoa错误3840.)(JSON文本没有启动与数组或
对象和选项允许片段不设置。)UserInfo = XXXXXXXXX
{NSDebugDescription = JSON文本没有开始与数组或对象和
选项允许片段未设置} / p>
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? Any idea? Thanks!
这是我发送数据并接收响应的函数:
Here's my function to send the data and receive the response:
- (void) putData:(NSString *)parametre valor:(NSString *)valor {
NSString *rawString = [NSString stringWithFormat:@"%@=%@", parametre, 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);
}
推荐答案
code> NSJSONReadingAllowFragments 到 [NSJSONSerialization JSONObjectWithData:options:error:]
来自服务器的响应必须是有效的JSON与顶层容器这是一个数组或字典。
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.
例如:
{ "response" : "Success" }
PS
如果你想要一个可变字典,你还必须在你的选项中包含 NSJSONReadingMutableContainers
。
这篇关于Cocoa错误3840使用JSON(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!