gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:32:15 27 4
gpt4 key购买 nike

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

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 格式,它开始正常工作。

关于ios - 使用 Alamofire 发布数据时出现 SerializationFailure 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41875919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com