【问题标题】:Alamofire can't get any result Swift 2Alamofire 无法得到任何结果 Swift 2
【发布时间】:2015-09-05 02:40:06
【问题描述】:

我正在寻找很多示例,主要来自网络上的@matt,但我无法获得这项工作:

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"

【问题讨论】:

  • 试图将 ``responseJSON ` 更改为 responseString
  • 嗨,有同样的行为。谢谢。
  • 哪个版本的 Alamofire(如果可以的话)和哪个版本的 Xcode?​​span>
  • pod 'Alamofire', :git => 'github.com/Alamofire/Alamofire.git', :branch => 'swift-2.0'

标签: swift swift2 alamofire


【解决方案1】:

你在控制台中得到任何输出吗?

看起来您正在使用为您的应用启用应用传输安全性的 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?

【讨论】:

  • 不是这样,请求只是没有给出任何答案
【解决方案2】:

我认为您的标题有问题。您应该更新您的请求。 您可以使用 Alamofire 2Swift 2 在请求中添加标头。

解决方案:go to the solution

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多