Ios 在swift2中使用Alamofire吊舱时POST请求不起作用?
我正在Ios 在swift2中使用Alamofire吊舱时POST请求不起作用?,ios,swift,xcode7.2,Ios,Swift,Xcode7.2,我正在swift2中做一个演示应用程序,它发出HTTPPOST请求。但是我不明白为什么我的请求没有到达服务器。 我使用的是阿拉莫菲尔3.0、Xcode 7.2、swift 2.0。 我根据以下内容安装了Alamofire吊舱: 我的绳子是 str = "{videos{title,videourl,imageurl}}" 我的代码是: Alamofire.request( .POST, url, parameters: Dictionary(), encodin
swift2
中做一个演示应用程序,它发出HTTP
POST
请求。但是我不明白为什么我的请求没有到达服务器。
我使用的是阿拉莫菲尔3.0、Xcode 7.2、swift 2.0。
我根据以下内容安装了Alamofire吊舱:
我的绳子是
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 = (
{
}
);
})
我不明白为什么它没有到达服务器。您以前做过吗?info.plist中的应用程序传输安全。 其次,参数没有任何东西。将字符串更改为字典应该是正确的 您可以使用json将字符串转换为字典
let data = changestring.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
var dict = NSDictionary()
do{
//dict = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
dict = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSDictionary
}
catch{
}
NSLog("this is dict \(dict)")
或者,您可以直接通过NSDictionary访问参数
Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
如果您希望使用HTTP POST和GET的其他方法,此链接适合您:
手动验证
Alamofire.validate(contentType:[“application/json”])可以搜索contentType的值,因为我不知道它可以/不能设置为contentType。您以前做过吗?info.plist中的应用程序传输安全。 其次,参数没有任何东西。将字符串更改为字典应该是正确的 您可以使用json将字符串转换为字典
let data = changestring.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
var dict = NSDictionary()
do{
//dict = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
dict = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSDictionary
}
catch{
}
NSLog("this is dict \(dict)")
或者,您可以直接通过NSDictionary访问参数
Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
如果您希望使用HTTP POST和GET的其他方法,此链接适合您:
手动验证
Alamofire.validate(contentType:[“application/json”])可以搜索contentType的值,因为我不知道它可以/不能设置为contentType。可能是服务器没有返回任何数据吗?您应该打印控制台中发出的请求的http响应,并检查您是否说“它没有击中服务器”,您是否确实检查了服务器日志?您的URL是否正确?@jcaron是的,我检查了服务器日志,而且我的URL在postman中工作正常。您的服务器URL是否为https?可能是服务器没有返回任何数据?您应该打印控制台中发出的请求的http响应,并检查您是否说“它没有击中服务器”,您是否确实检查了服务器日志?你的URL正确吗?@jcaron是的,我检查了服务器日志,而且我的URL在postman中工作正常。你的服务器URL是https吗?