【发布时间】:2015-12-29 09:44:20
【问题描述】:
我正在 Swift 2 中做一个演示应用程序,它发出一个 HTTP POST 请求。但是不明白为什么我的请求没有到达服务器。
我正在使用Alamofire 3.0、Xcode 7.2、swift 2.0。
我基于这个link安装了Alamofire pod:https://github.com/Alamofire/Alamofire#requirements
我的字符串是
str = "{videos{title,videourl,imageurl}}"
我的代码是:
Alamofire.request(
.POST,
url,
parameters: Dictionary(),
encoding: .Custom({
(convertible, params) in
let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
mutableRequest.setValue("application/graphql", forHTTPHeaderField: "Content-Type")
mutableRequest.HTTPBody = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
return (mutableRequest, nil)
}),
headers: nil
).responseJSON{
(JSON) -> Void in
if JSON.result.value != nil
{
print(JSON.result.value)
self.delegate.successReponse(JSON.result.value!, withType: type)
}
else
{
}
}
当我打印JSON.result.value 时,它显示如下:
Optional({
errors = (
{
}
);
})
我不明白为什么它没有到达服务器。
【问题讨论】:
-
可能是服务器没有返回任何数据?您应该打印在控制台中发出的请求的 http 响应并检查
-
你说“它没有命中服务器”,你真的检查过服务器日志吗?您的网址正确吗?
-
@jcaron 是的,我检查了服务器日志,而且我的网址在邮递员中也可以正常工作。
-
你的服务器网址是 https 吗?