我是iOS的新手,我想知道为什么我的代码无效。我正在尝试调用url并传递令牌并获得响应。我的回复是404状态代码。
let reverse = ["token": "831b21c47a7f7daee7d6e4e3fa11deaa"]
let url = "http://challenge.com"
Alamofire.request(url, parameters: reverse).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
答案 0 :(得分:0)
尝试以下代码:
Alamofire.request(url, method: .get, parameters: reverse, encoding: JSONEncoding.default).responseString { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
答案 1 :(得分:0)
试用此代码:
当响应为空并且禁用Internet连接时,此代码也会处理错误。
func apiCall(params : [String:AnyObject],block:([String:AnyObject]->Void)?) {
if Reachability.isConnectedToNetwork(){
Alamofire.request(.POST, URL, parameters: params, encoding: .JSON, headers: nil).responseJSON{
response in
let data = response.result.value
if data == nil{
if response.result.error?.code == -1005 {
print(response.result.error?.localizedDescription)
}
else{
switch response.result {
case .Success:
block!(data as! [String:AnyObject])
case .Failure(let error):
print(error)
}
}
}
}
else{
print(NO_NETWORK)
}
}