Alamofire的请求数据的方法:
public func request(_ url: URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil)
- 使用:
Alamofire.request(dataURL, method: .get, parameters: ["q": keyword, "count": "10"], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
}
- HTTPMethod
public enum HTTPMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
case trace = "TRACE"
case connect = "CONNECT"
}
- 编码 :
ParameterEncoding:
URLEncoding:
1 - URLEncoding和URL相关的编码,有两种编码方式:
直接拼接到URL中
通过request的httpBody传值
2 - JSONEncoding把参数字典编码成JSONData后赋值给request的httpBody
3 - PropertyListEncoding把参数字典编码成PlistData后赋值给request的httpBody
报错 "Extra argument 'method' in call"
一般会有这样几个原因:
1、url是字符串格式
2、parameters格式没写对,如果空写nil或[:]就行
3、encoding写错了