Error Domain = NSCocoaErrorDomain Code = 3840“无法使用AFNetworking在iOS中完成操作

问题描述 投票:1回答:1

我正在使用AFNetworking在iOS中使用post方法从服务器获取数据,但我得到状态代码200 OK但在AFNetworking中它出现在Failure方法中并且显示在下面错误我也在google上搜索我也试过基于结果但它显示相同的错误。

等待你的回复。

状态代码: -

{ status code: 200, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html"

错误: - 错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(可可错误3840。)”(字符0周围的值无效。)UserInfo = 0x7f838a5df950 {NSDebugDescription =字符0周围的值无效}

这是我的代码: -

NSError *error;
NSDictionary *result;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictPost
                options:kNilOptions    error:&error];
if (! jsonData) {
} else {

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

}

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager setResponseSerializer:responseSerializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];*


[manager POST:@“http:www.server.url.com” parameters:result success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON: %@", responseObject);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@“Error: %@", error.description);

}];
ios objective-c afnetworking
1个回答
0
投票

我认为你的问题与这个answer有关。

响应本身可能存在错误,无法将其解析为有效的JSON。

我尝试使用第三方工具调试服务器响应,例如Postman for Chrome。

更新:您的内容类型应为application/json

© www.soinside.com 2019 - 2024. All rights reserved.