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

时间:2017-01-26 14:38:57

标签: ios swift alamofire

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

  

FAILURE:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain = NSCocoaErrorDomain Code = 3840“字符0周围的值无效。”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)
    }
}

提前致谢

4 个答案:

答案 0 :(得分:6)

iOS代码是正确的,后端代码存在问题。 json没有正确形成。我纠正了后端的json形态,它开始工作正常。

答案 1 :(得分:2)

let headers:HTTPHeaders = ["Authorization": "Bearer " + token2Save]
let moreheaders:Parameters = ["Dropbox-API-Arg": ["path":sourcePath]]

Alamofire.request("https://content.dropboxapi.com/2/files/download", parameters: moreheaders, encoding: URLEncoding(destination: .queryString), headers: headers).responseJSON { feedback in
        guard feedback.result.value != nil else {
            print("Error: did not receive data", print("request \(request) feedback \(feedback)"))
            return

URLEncoding(目的地:.queryString),标题:标题).responseJSON

Replace responseJSON with responseString and check your response  from webservice.. it will lead to get the error line.

参见:

https://stackoverflow.com/a/43041931/5215474

  

将responseJSON替换为responseString并检查您的响应   webservice ..它将导致获取错误行。

答案 2 :(得分:1)

FrontEnd发送参数:

dataView

当我像这样 { "delivery_method": [{ "vehicle_type": "walk", "vehicle_id": "1", "vehicle_color": "", "vehicle_brand": "", "vehicle_image": "", "vehicle_number": "", "mode_type": "1", "vehicle_model": "" }, { "vehicle_type": "scooter", "vehicle_id": "4", "vehicle_color": "", "vehicle_brand": "", "vehicle_image": "", "vehicle_number": "", "mode_type": "1", "vehicle_model": "" }] } 解析JSON时抛出错误。这样我们可以为json创建一个参数。

JSON.parse(params[:delivery_method])

答案 3 :(得分:0)

检查网址中的路径可能是您缺少某些文件夹。