概述
iOS 端与服务端交互时,try JSON(data: data) 时出现无法解析 JSON 数据的问题。
error NSError domain: "NSCocoaErrorDomain" - code: 3840 0x00000002822bdf80
分析
debug 信息中,error 错误描述为 "JSON text did not start with array or object and option to allow fragments not set",即无法解析 JSON 数据。
此时,可将 data 数据转换成 String
let str = String(data: data, encoding: .utf8)
打印后发现,服务端返回的数据为
<html><title>405: Method Not Allowed</title><body>405: Method Not Allowed</body></html>
也即 HTTP 请求使用的方法错误,一般为 get方法用了 post 方法或者相反
解决方案
更改使用的 HTTP 请求的方法即可
网友评论