【发布时间】:2016-08-25 10:17:36
【问题描述】:
我正在使用这样的 post 方法发送数据
let login = ["user_name":usernameTextField.text,"password":passwordTextField.text]
//["user":"ords_user@gmail.com", "pass":"ords_password"]
let url = NSURL(string: "http://localhost:8300")!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
do {
// JSON all the things
let auth = try NSJSONSerialization.dataWithJSONObject(login, options: .PrettyPrinted)
// Set the request content type to JSON
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// The magic...set the HTTP request method to POST
request.HTTPMethod = "POST"
// Add the JSON serialized login data to the body
request.HTTPBody = auth
// Create the task that will send our login request (asynchronously)
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
// Do something with the HTTP response
print("Got response \(response) with error \(error)")
print("Done.")
})
// Start the task on a background thread
task.resume()
} catch {
// Handle your errors folks...
print("Error")
}
但我收到类似的错误消息
参数类型“[String : String?]”不符合预期的类型“AnyObject”
如果我给出它接受的直接字符串。如果我使用 TextFields 动态地给出它就不会出现。我不知道我犯了什么错误。
谁能帮忙解决这个问题?
提前致谢。
【问题讨论】:
-
您在哪一行得到实际错误?
-
let auth = try NSJSONSerialization.dataWithJSONObject(login, options: .PrettyPrinted) at "login"
-
解开你的 textField 的文本。
标签: ios swift nsdictionary nsjsonserialization