這種變種工作
let parameters = [
"foo": "bar",
"baz": ["a", 1],
"qux": [
[["x": 1],["y": 2]],
[["x": 1],["y": 2]],
[["x": 1],["y": 2]]
]
]
let data = NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions.PrettyPrinted,error:nil)
let post = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
httpPost("http://httpbin.org/post", postData: post){res, code in
println(res)
}
//將函數代碼上,你可以發佈您的數據
func httpPost(url:String, postData: String, completion: (String, Int) -> Void) {
var request = NSMutableURLRequest(URL: NSURL(string: url)!)
request.HTTPMethod = "POST"
request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding);
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36", forHTTPHeaderField: "User-Agent")
request.addValue("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", forHTTPHeaderField: "Accept")
request.addValue("gzip, deflate, sdch", forHTTPHeaderField: "Accept-Encoding")
request.addValue("max-age=0", forHTTPHeaderField: "Cache-Control")
var session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: { data, response, error in
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode
completion(NSString(data: data, encoding: NSUTF8StringEncoding) as! String, statusCode)
}
})
task.resume()
}
意思「巴茲」:[「A」:1]? – Azzaknight