简体   繁体   English

无法让 POST 请求工作 AlamoFire 和 XCode/Swift

[英]Cannot get the POST request to work AlamoFire and XCode/Swift

I am using Swift and Xcode and am trying to send a Post request.我正在使用 Swift 和 Xcode 并尝试发送 Post 请求。 A normal request without parameters and no method declaration is working.没有参数且没有方法声明的正常请求正在工作。 However, when I try to do a specific post request with parameters it no longer works.但是,当我尝试使用参数执行特定的发布请求时,它不再起作用。 I have taken the logic straight from Alamofire's documentation.我直接从 Alamofire 的文档中获取了逻辑。 I have included some pictures and code below.我在下面包含了一些图片和代码。

来自 XCode 的屏幕截图

let parameters = ["barcdodeNumber": displayValue]
            
AF.request(url, method: .post, parameters: parameters)
//this is the right way, but it does not work
            
AF.request(url).response { response in
    debugPrint(response)
}
//this is wrong but it works

You need to call one of the response methods, eg,您需要调用其中一种response方法,例如,

AF.request(url, method: .post, parameters: parameters).response { response in
    debugPrint(response)
}

This begs the question as to what your endpoint returns.这就引出了关于您的端点返回什么的问题。 Usually it's JSON, so you'd use responseJSON or one of the decoder renditions.通常它是 JSON,因此您可以使用responseJSON或解码器格式之一。

Ultimately this was the way that ended up working correctly.最终,这就是最终正常工作的方式。

    AF.request(url, method: .post, parameters: params, encoding: 
    URLEncoding(destination: .queryString)).response { response in
                debugPrint(response)
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM