2015-09-05 94 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