错误代码:错误域=NSCocoaErrorDomain 代码=3840 "JSON text did not start with array or object and option to allow fragments not set."
Error Code : Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set."
我在使用 afNetworking PUT 方法时遇到了这个错误。请帮忙。
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start
with array or object and option to allow fragments not set."
UserInfo={NSDebugDescription=JSON text did not start with array or
object and option to allow fragments not set.}
这是我的代码:-
NSInteger intUserID = [[prefs stringForKey:@"user_id"] integerValue];
NSInteger intProdID = [_strProdID integerValue];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;
NSDictionary *params = @{@"In d":@(1),@"user_id":@(intUserID),@"product_id":@(intProdID)};
[manager PUT:@"http://api.XXX.com/api/product/wishlist_add" parameters:params success:^(NSURLSessionTask *task, id responseObject) {
}failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error Code : %@", error);
}];
It clearly says you have sent wrong JSON format. Means there is two
possibility.
要么你发错了json.
或者您的服务器无法解析您请求的数据。
我已经遇到过同样的问题。请使用下面的代码,我相信它对你有用。
NSInteger intUserID = [[prefs stringForKey:@"user_id"] integerValue];
NSInteger intProdID = [_strProdID integerValue];
NSDictionary *params = @{@"In d":@(1),@"user_id":@(intUserID),@"product_id":@(intProdID)};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;
[manager PUT:@"http://api.XXX.com/api/product/wishlist_add" parameters:jsonString success:^(NSURLSessionTask *task, id responseObject) {
}failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error Code : %@", error);
}];
}
从 here
中查找参考
更新:
它工作正常。请参阅下面的截图。
请确保 user_id
和 product_id
值不应为 nil
出现这个错误主要有两个原因。
服务器无法解析您请求的数据
发送错误请求
首先尝试在邮递员上调用网络服务。如果您的网络服务调用成功并且您获得响应数据,则使用 json 验证器验证该 json 响应。
如果 json 响应有效,则检查您的请求。
大多数时候问题出在我们发送给服务器的请求上。
编码愉快。
我在使用 afNetworking PUT 方法时遇到了这个错误。请帮忙。
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
这是我的代码:-
NSInteger intUserID = [[prefs stringForKey:@"user_id"] integerValue];
NSInteger intProdID = [_strProdID integerValue];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;
NSDictionary *params = @{@"In d":@(1),@"user_id":@(intUserID),@"product_id":@(intProdID)};
[manager PUT:@"http://api.XXX.com/api/product/wishlist_add" parameters:params success:^(NSURLSessionTask *task, id responseObject) {
}failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error Code : %@", error);
}];
It clearly says you have sent wrong JSON format. Means there is two possibility.
要么你发错了json.
或者您的服务器无法解析您请求的数据。
我已经遇到过同样的问题。请使用下面的代码,我相信它对你有用。
NSInteger intUserID = [[prefs stringForKey:@"user_id"] integerValue];
NSInteger intProdID = [_strProdID integerValue];
NSDictionary *params = @{@"In d":@(1),@"user_id":@(intUserID),@"product_id":@(intProdID)};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;
[manager PUT:@"http://api.XXX.com/api/product/wishlist_add" parameters:jsonString success:^(NSURLSessionTask *task, id responseObject) {
}failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error Code : %@", error);
}];
}
从 here
中查找参考更新:
它工作正常。请参阅下面的截图。
请确保 user_id
和 product_id
值不应为 nil
出现这个错误主要有两个原因。
服务器无法解析您请求的数据
发送错误请求
首先尝试在邮递员上调用网络服务。如果您的网络服务调用成功并且您获得响应数据,则使用 json 验证器验证该 json 响应。 如果 json 响应有效,则检查您的请求。
大多数时候问题出在我们发送给服务器的请求上。
编码愉快。