我可以问一下hepl吗?尝试从 .responseJSON 获取结果但收到响应失败。
func performRequest(_ method: HTTPMethod, requestURL: String, params: [String: AnyObject], comletion: @escaping (_ json: AnyObject?) -> Void) {
Alamofire.request(requestURL, method: .get, parameters: params, encoding: JSONEncoding.default)
.responseJSON { response in
print(response)
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
}
print("data:", response.data ?? "no data")
print("result:", response.result)
if let result = response.result.value {
let JSON = result as! NSDictionary
print("JSON:",JSON)
comletion(JSON)
}
comletion(nil)
}
}
Run Code Online (Sandbox Code Playgroud)
我的回答是:
data: 66809 bytes
result: FAILURE
json:
Run Code Online (Sandbox Code Playgroud)
我也尝试使用其他类型的响应,如 .responseData 但由于某种原因无法解析。
let json = JSON(data: response.data!) //getting nil
Run Code Online (Sandbox Code Playgroud)
谢谢
尝试这个,
func performRequest(_ method: HTTPMethod, requestURL: String, params: [String: AnyObject], comletion: @escaping (_ json: AnyObject?) -> Void) {
Alamofire.request(requestURL, method: .get, parameters: params, headers: nil).responseJSON { (response:DataResponse<Any>) in
print(response)
switch(response.result) {
case .success(_):
if let data = response.result.value{
print("YOUR JSON DATA>> \(response.data!)")
comletion(nil)
}
break
case .failure(_):
print(response.result.error)
comletion(nil)
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5180 次 |
| 最近记录: |