【问题标题】:getting nil value while calling api (alamofire)调用 api (alamofire) 时获取 nil 值
【发布时间】:2016-01-11 09:11:37
【问题描述】:

我正在使用 alamofire 调用 api。但在这个特定的 api 中,我得到 nil 值作为响应。实际上,当我在浏览器中点击我的 api 时,它工作得很好。

这是我的代码

 func web()
{


    request(.GET, "http://www.horecasupply.nl/AJAX?function=appStructure", parameters: nil, encoding: .JSON).responseJSON { (response : Response<AnyObject, NSError>) -> Void in
        if response.result.value != nil {
            print(response.result.value)
        }

    }

}

得到 nil 值,当我打印时,它会显示给我

Printing description of response.result.Failure:
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1."         
UserInfo={NSDebugDescription=Invalid value around character 1.}

编辑 1:: 尝试了 responsestring 而不是 responsejson。 但是得到不同类型的响应,那么我该如何转换呢?

\r\n\r\n\r\n\r\n\r\n DD_belatedPNG.fix(\'img, .png_bg\'); //修复任何或 .png_bg 背景图像 \r\n\r\n\r\n\r\n-->\r\n\r\n\ r\n\r\n\r\n\r\n \r\n\r\n")

【问题讨论】:

  • 尝试使用responseString 代替responseJson Alamofire invalid value around character 0
  • 通过使用responsestring得到这种响应\r\n\r\n\r\n\r\n\r\n DD_belatedPNG.fix(\'img, .png_bg \'); //修复任何或 .png_bg 背景图像 \r\n\r\n\r\n\r\n-->\r\n\r\n\ r\n\r\n\r\n\r\n \r\n\r\n")
  • 我运行了你发布的相同代码并且它工作正常。我得到与浏览器相同的响应。
  • 你使用的是哪个xcode?呸呸呸
  • 您的代码 sn-p 对我有用 (screenshot)。

标签: ios swift swift2 ios9 alamofire


【解决方案1】:

试一下这段代码,它应该可以工作:

    Alamofire.request(.GET, "http://www.horecasupply.nl/AJAX?function=appStructure").responseJSON { response in
    if response.result.value != nil {
        print(response.result.value)
    }
}

【讨论】:

  • 为什么?如何? OP 的代码对我有用,为什么你认为用你的代码替换它会改善他们的情况?请解释你的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多