Alamofire发布序列化错误

时间:2018-03-14 16:06:37

标签: http-post alamofire swift4

我正在尝试使用alamofire创建HTTP Post,下面是我的代码:

@IBAction func loginUserBtn(_ sender: Any) {

    let urlAddress = "http://xx.xx.xx.xx:xx/xx/xx/xx/login"
    let pass = Array(userPassword.text!)
    let username = userEmail.text!

    let params : [String: Any] = ["username": username, "pwd":  pass]
    print(params)

    let headers: HTTPHeaders = [
        "Accept": "application/json",
        "Content-Type": "application/json"
    ]


    Alamofire.request(urlAddress, method: .post, parameters: params, encoding: URLEncoding.default, headers: headers).responseJSON{

        responds in
        if responds.result.isSuccess{
            print("Succesfull")

            let LoginJSON : JSON = JSON(responds.result.value!)
            //self.updateWeatheData(json: weatherJSON)
            print(LoginJSON)
        }
        else{
            print("Error \(String(describing: responds.result.error))")


        }
    }

}

这是我遇到的错误,但是如果我将responseJSON更改为responseString,我获得了成功,但服务器拒绝了我的数据。

这是我的错误消息:

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

1 个答案:

答案 0 :(得分:0)

encoding: URLEncoding.default

我看到你的内容类型标题是JSON,但编码字段告知你的params是什么类型。试试这个:

encoding: JSONEncoding.default