没有得到使用alamofire的post方法的正确响应,结果是失败

时间:2017-03-14 06:08:27

标签: swift post alamofire

对于使用Alamofire的帖子方法,我没有得到正确答案。 结果是失败,在响应内容类型获取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,分别为mobile和pin 但是我想为这个如何做到这一点给出动态值。

1 个答案:

答案 0 :(得分:0)

您必须在某个变量中包含mobilepin的值。 可以说,strMobilestrPin只需在字典中替换它。

let params = ["mobile": strMobile, "pin":strPin, "devicekey":"99", "deviceOs":"123456", "deviceOsVersion":"12", "userType":"employee"]

或者使用var代替let,然后填写键值。

var params = ["devicekey":"99", "deviceOs":"123456", "deviceOsVersion":"12", "userType":"employee"]
params["mobile"] = strMobile
params["pin"] = strPin