2015-09-05 86 views
0

我找了很多的例子,主要是從網絡上的任何@馬特的結果,但我不能得到這個工作:Alamofire不能得到斯威夫特2

import Alamofire 

extension Request { 
    public func debugLog() -> Self { 
     debugPrint(self) 
     return self 
    } 
} 

func login(userName: String, passWord: String) -> String { 
    let manager = Manager.sharedInstance 
    // Specifying the Headers we need 
    manager.session.configuration.HTTPAdditionalHeaders = [ 
     "Content-Type": "application/json", 
     "User-Agent": "test", 
     "Cache-Control": "no-cache", 
     "Authorization": "apikey" 
    ] 
    // When 

    let params = 
     ["userName" : userName, 
     "passWord" : passWord] 

    Alamofire.request(.POST, Constants.apiURL.url + "users/login", parameters: params) 
     .validate() 
     .debugLog() 
     .responseJSON { responseRequest, responseResponse, responseResult in 
      NSLog(String(responseRequest)) // never enter here 
      NSLog(String(responseResponse)) 
      NSLog(String(responseResult)) 
    } 

    return "" 
} 

它從來沒有進入裏面我阻止.responseJSON。無論我用什麼參數,強制錯誤或從調試中獲得完美的cURL命令,它都不會起作用。

我的cURL很好,我可以在bash中正常執行它,並且我得到了一個很好的json結果。

curl -i \ 
    -X POST \ 
    -H "Content-Type: application/x-www-form-urlencoded" \ 
    -H "Cache-Control: no-cache" \ 
    -H "User-Agent: test" \ 
    -H "Authorization: apikey" \ 
    -H "Content-Type: application/json" \ 
    -d "passWord=123&userName=123" \ 
    "http://api.cc/users/login" 
+0

試圖''responseJSON改變''到responseString' – Bannings

+0

嗨,具有相同的行爲。謝謝。 – GIJOW

+0

Alamofire的哪個版本(如果可以的話)以及哪個版本的Xcode? – cnoon

回答

0

你是否在控制檯中獲得任何輸出?

看起來您正在使用Xcode 7,併爲您的應用啓用了應用傳輸安全併發送了HTTP請求。嘗試通過添加以下內容到Info.plist中忽略ATS限制:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 

更多細節在這裏:How do I load an HTTP URL with App Transport Security enabled in iOS 9?

+0

這不是這種情況,請求只是不給任何答案 – GIJOW

0

我覺得喲有頭有問題。你應該更新你的請求。 您可以在Alamofire 2Swift 2的請求中添加您的標題。

對於解決方案:go to the solution