我張貼NSURLMutableRequest
同時使用Alamofire和類似的自定義方法的WebAPI:差異的NSURLRequest
Alamofire
Alamofire.request(.POST, "http://www.webapi.com?path=Login", parameters: ["username": "myname", "password": "mypass"]).responseJSON { response in
// do something with returned data ...
}
定製
let request = NSMutableURLRequest(URL: NSURL(string:"http://www.webapi.com?path=Login")!)
let bodyData = "username=myname&password=mypass"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(request) { (data, response, error) -> Void in
// do something with returned data...
}.resume()
我通過Alamofire方法得到了正確的結果,但不是我自己的 方法。
與Alamofire post方法相比,我做了什麼錯誤?
可以打開Alamofire.request方法。 alamofire的代碼是開源的。 – Sahil
當你說你得到了正確的結果; NSURLRequest的錯誤結果是什麼?這是一個崩潰,從任務返回的錯誤,錯誤的數據? – keithbhunter
@keithbhunter正確的結果是json格式化的數據和不正確的結果只是'無' – bluenowhere