Swift 4 Alamofire响应序列化失败

时间:2019-01-01 01:24:29

标签: swift alamofire

我正在尝试使用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)
        }
    }
}

0 个答案:

没有答案