[英]POST request not working while using Alamofire pod in swift2?
I'm doing a Demo App in Swift 2
that makes an HTTP
POST
request. 我正在Swift 2
中做一个演示应用程序,该应用程序发出HTTP
POST
请求。 But don't understand why my request doesn't hit the server. 但是不明白为什么我的请求没有到达服务器。 I'm using Alamofire 3.0
, Xcode 7.2
, swift 2.0
. 我正在使用Alamofire 3.0
, Xcode 7.2
和Alamofire 3.0
swift 2.0
。 I installed the Alamofire
pod based on this link : https://github.com/Alamofire/Alamofire#requirements 我根据此链接安装了Alamofire
吊舱: https : //github.com/Alamofire/Alamofire#requirements
And my string is 我的弦是
str = "{videos{title,videourl,imageurl}}"
Here my code is: 这是我的代码:
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
{
}
}
When i'm printing the JSON.result.value
, it's showing like this: 当我打印JSON.result.value
,它显示如下:
Optional({
errors = (
{
}
);
})
I don't understand why it is not reaching to the server. 我不明白为什么它无法到达服务器。
Have your do it before ? 你以前做过吗? App Transport Security in info.plist. info.plist中的App Transport Security。 https://github.com/Alamofire/Alamofire Secondly, the parameters 's don't have any things. https://github.com/Alamofire/Alamofire其次,参数没什么东西。 It should be change your string to Dictionary. 应该将您的字符串更改为Dictionary。
Your can use json to make string to Dictionary. 您可以使用json将字符串转换为Dictionary。
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)")
Or you can just past NSDictionary to parameters. 或者,您也可以将NSDictionary传递给参数。
Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
If your want to others method for HTTP POST and GET, this link is Suitable for you: https://github.com/RYANCOAL/swift-with-AFNetworking/blob/master/TestHTTPTests/TestHTTPTests.swift 如果您希望使用其他方法进行HTTP POST和GET,则此链接适合您: https : //github.com/RYANCOAL/swift-with-AFNetworking/blob/master/TestHTTPTests/TestHTTPTests.swift
Manual Validation Alamofire.validate(contentType: ["application/json"]) can search the value for contentType, by I don't know it can / cannot be set contentType. 手动验证Alamofire.validate(contentType:[“ application / json”])可以搜索contentType的值,因为我不知道它可以/不能设置contentType。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.