【发布时间】:2017-03-14 06:08:26
【问题描述】:
对于使用 Alamofire 的 post 方法,我没有得到正确的响应。 结果是失败,并且响应内容类型获取 text/xml 而不是 application/json,任何人都可以帮我解决这个问题。
`
let params = ["mobile":"0000000000", "pin":"000000", "devicekey":"99", "deviceOs":"123456", "deviceOsVersion":"12", "userType":"employee"]
var request = URLRequest(url: Login_URL! as URL)
request.httpMethod = "POST"
request.setValue ("application/json",forHTTPHeaderField: "Content-Type")
let data = try! JSONSerialization.data(withJSONObject: params as Any, options: JSONSerialization.WritingOptions.prettyPrinted)
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if let json = json {
print(json)
}
request.httpBody = json!.data(using: String.Encoding.utf8.rawValue);
Alamofire.request("Login_URL",method: .post, parameters: params, encoding: JSONEncoding.default)
.responseJSON(options: .allowFragments) { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
print("Data: \(response.data)")
print("Result: \(response.result)")
if let JSON = response.result.value {
print("JSON: \(JSON)`
这里我给的参数是 0000000000 和 000000 分别用于移动和 pin 但我想为此提供动态值,我该怎么做。
【问题讨论】:
-
您能否展示您尝试过的代码以及收到的错误类型?
-
嗨,我刚得到输出,是 url 拼写错误
-
嗨,你能解释一下我们如何动态获取参数吗,例如:我有用户名和密码,应该是动态的,你能解释一下如何使用 post 方法@Dhruv
-
@vineela 你问的是向 alamofire 添加参数吗?
-
@vineela 至少显示您的代码,然后我们很容易根据您的实现提出建议。