首页 > 解决方案 > Swift 4 alamofire responseSerializationFailed

问题描述

我正在尝试使用 alamofire 发送请求 .post 但它给了我这个错误:

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

我在邮递员那里试过,它工作正常,但在代码中给了我那个错误,我使用的是 swift 4.2

class func sendMsg(toUserId:String ,message: String, completion: @escaping (_ error: Error?, _ success: Bool)->Void ) {

    //let url = URLs.sendMessage
    let url = "http://tdlly.com/Api/SendMessage/18"
    let paramaters = [
        "to_user_id": toUserId,
        "message_content": message,
        ]

    Alamofire.request(url, method: .post, parameters: paramaters, encoding: URLEncoding.default, headers: nil).responseJSON { response in

        switch response.result {
        case .failure(let error):
            completion(error, false)
            print(error)
        case .success(let value):
            let data = JSON(value)
            print(data)
            if  (data["success"].int == 1) {
                SVProgressHUD.show(UIImage(named: "cor.png")!, status: "message Sent")
                SVProgressHUD.setShouldTintImages(false)
                SVProgressHUD.setImageViewSize(CGSize(width: 40, height: 40))
                SVProgressHUD.setFont(UIFont.systemFont(ofSize: 20.0))
                SVProgressHUD.dismiss(withDelay: 2.0)
            } else if (data["success"].int == 0) {
                SVProgressHUD.show(UIImage(named: "er.png")!, status: "message not Sent")
                SVProgressHUD.setShouldTintImages(false)
                SVProgressHUD.setImageViewSize(CGSize(width: 40, height: 40))
                SVProgressHUD.setFont(UIFont.systemFont(ofSize: 20.0))
                SVProgressHUD.dismiss(withDelay: 2.0)
            } else if (data["success"].int == 2) {
                SVProgressHUD.show(UIImage(named: "emp.png")!, status: "message empty")
                SVProgressHUD.setShouldTintImages(false)
                SVProgressHUD.setImageViewSize(CGSize(width: 40, height: 40))
                SVProgressHUD.setFont(UIFont.systemFont(ofSize: 20.0))
                SVProgressHUD.dismiss(withDelay: 2.0)
            }
            completion(nil ,true)
        }
    }
}

标签: swiftalamofire

解决方案


推荐阅读