xcode 仅在 GET 请求上出现 Alamofire 请求错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34421809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alamofire Request error only on GET requests
提问by Cody Winton
I'm working on transferring my project from AFNetworking to Alamofire. Really like the project. POST requests work just fine, however, I'm receiving this error when attempting to make a GET request.
我正在将我的项目从 AFNetworking 转移到 Alamofire。真的很喜欢这个项目。POST 请求工作正常,但是,我在尝试发出 GET 请求时收到此错误。
Here's some example code:
下面是一些示例代码:
class func listCloudCredntials(onlyNew onlyNew: Bool = true, includePending: Bool = true) -> Request {
let parameters: [String: AnyObject] = includePending ? ["include_pending": "true"] : [:]
let urlString = "https://myapp-staging.herokuapp.com/api/1/credntials"
let token = SSKeychain.storedToken()
let headers: [String: String] = ["Authorization": "Bearer \(token)"]
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON, headers: headers)
}
I receive this error: : -1005 The network connection was lost
我收到此错误:: -1005 The network connection was lost
However, if I change the request type to .POST
, the request "works". I receive a 401 code, but at least the request doesn't lose Network connection.
但是,如果我将请求类型更改为.POST
,则请求“有效”。我收到 401 代码,但至少请求不会丢失网络连接。
What am I doing wrong?
我究竟做错了什么?
回答by redent84
You're encoding the parameters as JSON in the body of the request, try encoding the parameters in the URL by changing the encoding to URL
:
您将请求正文中的参数编码为 JSON,尝试通过将编码更改为 对 URL 中的参数进行编码URL
:
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .URL, headers: headers)
As this is the default behavior, you can simply remove it:
由于这是默认行为,您可以简单地将其删除:
return Alamofire.request(.GET, urlString, parameters: parameters, headers: headers)