SerializationFailure在使用Alamofire发布数据时发生错误
我试图使用Alamofire保存一些文本数据和图像到服务器,但我得到以下错误:
FAILURE:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain = NSCocoaErrorDomain Code = 3840“Invalid value around character。”UserInfo = {NSDebugDescription =字符0周围的值无效。
我的代码:
internal func postContent(forApi Name:String, image:UIImage?, withData payload:[String: String], success: ((_ response:[String: AnyObject])->Void)?, failure: ((Error)->Void)?) { //create Alamofire request //if everything was fine call success block with people array passed into it //if failure occurred, call failure block with error. if(isConnectedToNetwork()){ let url = SharedConstants.baseURL+Name print("url "+SharedConstants.baseURL+Name) Alamofire.upload(multipartFormData: { (multipartFormData) in if let img = image { multipartFormData.append(UIImageJPEGRepresentation(img, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg") } for (key, value) in payload { multipartFormData.append(value.data(using: .utf8)!, withName: key) } }, to: url, method: .post , headers:nil, encodingCompletion: { (result) in switch result { case .success(let upload, _, _): upload.responseJSON(completionHandler: { (response) in print(response.request) // original URL request print(response.response) // URL response print(response.data) // server data print(response.result) // result of response serialization if let JSON = response.result.value { print(JSON) success!(JSON as! [String: AnyObject]) } else{ failure!(ErrorType.noRecordFound) } }) case .failure(let error): print(error) } }) } else{ failure!(ErrorType.internetNotWorking) } }
提前致谢
你可以得到与ios代码的错误只需按照我的答案在这里
https://stackoverflow.com/a/43041931/5215474
用responseStringreplaceresponseJSON,并检查你的webservice的响应..这将导致获取错误行。
iOS代码是正确的,后端代码存在问题。 json的forms不正确。 我纠正了后端json的形成,并开始正常工作。
- 收到错误:Error Domain = com.facebook.sdk.core Code = 8“(null)”
- 我如何使用iOS SDK的Swift REPL
- Swift 4 CTRubyAnnotation不起作用
- 为什么在状态恢复后改变我的应用程序的方向来取消我的父视图控制器?
- waitUntilAllTasksAreFinished错误Swift
- 单击第一个ViewController中的button,更改第二个ViewController的标签文本
- Google地图在标记信息窗口中显示speechbubble iOS
- dynamic更改TableView单元格高度
- 谷歌分析电子商务价值并没有在谷歌分析控制台更新