2016-09-29 33 views
1

我是iOS的新手,我想知道爲什麼我的代碼無法正常工作。我正在嘗試撥打一個URL並傳遞一個令牌並獲得響應。我的回覆是404狀態代碼。使用Alamofire傳遞令牌和發佈請求

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

您使用的是alamofire的哪個版本? –

+0

版本4 @EktaMakadiya –

+0

我假設您的請求是GET。對? –

回答

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)") 
     } 
    } 
+0

一旦從手機中刪除應用程序並再次運行。 –

+0

我將方法更改爲.post並且我有一個響應,但它不是我應該得到的json響應,並且如果讓JSON = response.result.value {「JSON:\(JSON)」) }未運行 –

+0

print(response.result)。告訴我結果。 –

0

試試這個代碼:

此代碼也處理錯誤時的反應將是空白的,還互聯網連接禁用。

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) 
     } 
    }