使用Alamofire发布数据时出现SerializationFailure错误

编程入门 行业动态 更新时间:2024-10-21 16:00:00
本文介绍了使用Alamofire发布数据时出现SerializationFailure错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Alamofire将一些文本数据和图像保存到服务器,但是出现以下错误:

I am trying to save some text data and an image to server using Alamofire but I am getting following error:

失败:responseSerializationFailed (Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域= NSCocoaErrorDomain代码= 3840字符0周围的无效值。 UserInfo = {NSDebugDescription =字符0周围的无效值。}))

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 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代码正确,后端代码存在问题。 json格式不正确。我更正了后端的json格式,它开始正常工作。

The iOS code is correct, there was problem in backend code. The json was not being properly formed. I corrected the json formation in backend and it started working fine.

更多推荐

使用Alamofire发布数据时出现SerializationFailure错误

本文发布于:2023-11-26 18:04:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   数据   Alamofire   SerializationFailure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!