【发布时间】:2018-04-08 19:41:33
【问题描述】:
我正在开发 swift 项目并使用 Alamofire 调用 web 服务。 但是,在调用 post 方法时,我收到以下错误。
Header file :
let accessTokenHeaderFile = [
"Accept": "application/json",
"Content-Type" :"application/json",
"X-TOKEN" : UtilityClass.sharedInstance.accessTokenString
]
Alamofire.request(urlString, method: .post, parameters: params as? [String:Any], encoding: JSONEncoding.default, headers: accessTokenHeaderFile).responseJSON { response in
requestVC.removeLoader()
switch (response.result) {
case .success:
if response.result.value != nil{
completionHandler (response.result.value)
}
break
case .failure(let error):
failureHandler (error as NSError?)
break
}
}
错误是
FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
任何人都可以建议我,如何解决这个问题,我尝试使用谷歌搜索,但无论我找到什么答案都对我没有帮助。
【问题讨论】:
-
响应似乎不是有效的 JSON。您能否尝试获取 responseString 而不是 responseJSON 并检查其 JSON 有效性?
-
我是 swift 新手,能否请您指导我如何从 responseJSON 更改为 responseString?
-
你可以尝试打印返回值并检查JSON语法是否正确吗?
标签: ios iphone swift xcode alamofire