App 使用很久了,昨天出现一个bug,网络请求总失败。调试打印,发现是这样的错误:
AFHTTPRequestOperation *operation = [manager POST:URLString parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (successBlock) {
successBlock(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failureBlock) {
failureBlock(error);
}
NSLog(@"%@", error);
}];
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., NSUnderlyingError=0x15eb52e0 {Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad gateway (502)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x15d4c7d0> { URL: http://xxx.com } { status code: 502, headers { Connection = "keep-alive"; "Content-Length" = 173; "Content-Type" = "text/html"; Date = "Mon, 01 Feb 2016 03:35:51 GMT"; Server = "nginx/1.5.12"; } }, NSErrorFailingURLKey=http://xxx.com, NSLocalizedDescription=Request failed: bad gateway (502)
有大神遇到过吗?App 使用很久了,头一次出现这样的 bug, 因此可以排除 manager 的配置问题。
The data returned by the server is not in json format. Just let the backend adjust it. This is the most common mistake.
When this happens, the most common reason is that the interface adds printing information, or the interface throws an error, causing the data returned by the request to be not legal JSON and an error is reported
Correct answer on the first floor, this problem has been encountered before, that is, the data returned by the interface is not in standard json format, which may be caused by line breaks or other symbols.
@Aster0id, @biaoge, @Na爱利懇 are indeed caused by the server. Thanks.