Alamofire Post Request未执行

时间:2017-07-20 12:31:44

标签: swift swift3 xcode8 alamofire swifty-json

我正在向我的API发出POST请求。突然间,请求被跳过了。我试图调试它,但直到现在都没有成功。

这是我的要求:

@IBAction func checkLogin(_ sender: Any) {
    guard let managedContext = self.managedObjectContext else { return }

    let user = NSEntityDescription.insertNewObject(forEntityName: User.identifier, into: managedContext) as! User

    let url = ""

    let parameters: Parameters =
        ["username" : usernameTextField.text!, "password" : passwordTextField.text!]


     Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { (responseData) -> Void in

        let results = JSON(responseData.result.value!)
        print(results)

        user.firstName = results["firstname"].string!

        let responseString : String = responseData.response?.allHeaderFields["Set-Cookie"] as! String

        if let range = responseString.range(of: ";"){
            let startIndex = (responseString.range(of: "="))

            let cookie = responseString[(startIndex?.upperBound)!...range.lowerBound]
            user.setValue(cookie, forKey: "token")

        }
    }  do {
        try self.dataController.saveContext()
    }catch {
        print("Save Error User")
    }

我使用Alamofire 4.5和Swift 3.1。

1 个答案:

答案 0 :(得分:1)

请使用不同类型的数据请求处理块并再次检查。

Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default)

.responseJSON { response in
    print("JSON Response")
}
.responseData { response in
    print("Data Response")              
}
.responseString { response in
    print("String Response")
}
.responsePropertyList { response in
    print("PropertyList Response")
}