简体   繁体   English

在 Swift 中使用 Alamofire 发送请求时如何在正文中传递参数

[英]how to pass parameters in body when sending request with Alamofire in Swift

I have an api (nodejs) which saves my application data in MongoDB.我有一个 api (nodejs),它将我的应用程序数据保存在 MongoDB 中。 But when I send the request, node throws an error which says that score is undefined但是当我发送请求时,节点会抛出一个错误,指出score undefined

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'score' of undefined

And when Xcode sends the request, the application works for about 30 seconds and then crashes and Xcode logs: "The request timed out."当 Xcode 发送请求时,应用程序工作大约 30 秒然后崩溃,Xcode 记录: "The request timed out."

this is my Swift code:这是我的 Swift 代码:

let parameters = ["score": scoreBoard]
print(parameters)
AF.request("http://localhost:4000/update/score", method: .post, parameters: parameters).responseDecodable(of: [jsonData].self) {response in
                      let json = JSON(response.data!)[0]["score"]
                      scoreBoard = json.rawString()!
                }

try with this one:试试这个:

Alamofire.request(apiurl, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseJSON {
    (response:DataResponse) in
    print("response - ",response)
}

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

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