FAILURE:responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

问题描述 投票:0回答:1

我正在尝试使用Alamofire阅读JSON。这是我的代码:

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

    func getConfirmationCode(params: [String:Any], block: @escaping(_ data : JSON, _ success : Bool) -> Void) {
    if let url = URL(string: baseURL + "getConfirmationCode.php") {
        Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
            DispatchQueue.main.async {
                if let value = response.result.value {
                    let json = JSON(value)
                    block(json, true)
                } else {
                    block(JSON.null, false)
                }
            }
        }
    }
}

JSON是有效的,一切都在Postman中完美运行,但我无法使它工作。 Web上的任何建议都没有帮助(将responseJSON更改为responseString / responseData,将JSONEncoding.default更改为URLEncoding.default等)

谁能帮我?

ios json swift alamofire
1个回答
0
投票

您应该尝试在响应部分的“预览”选项卡中检查邮递员的响应。有时它会在“漂亮”部分显示正确的json。但在“预览”部分中,它包含一些API响应的打印语句。那些打印语句在json解码中产生问题。

enter image description here

另外,如果你想获得快速的邮递员回复代码,你总能从邮递员的代码功能中得到它:

enter image description here

enter image description here

希望这可以帮助..!

© www.soinside.com 2019 - 2024. All rights reserved.