發佈的數據我使用POST方法類似這樣的得到錯誤在迅速
let login = ["user_name":usernameTextField.text,"password":passwordTextField.text]
//["user":"[email protected]", "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")
}
發送的數據,但我越來越喜歡
參數類型「錯誤信息[字符串:字符串] '不符合預期類型'AnyObject'
如果我給直接字符串它接受。如果我動態使用TextField,它不會來。我不知道我做了什麼錯誤。
任何人請幫助解決這個問題?
在此先感謝。
哪條線你得到實際的錯誤? – sbarow
let「auth = try」NSJSONSerialization.dataWithJSONObject(login,options:.PrettyPrinted)at「login」 –
Unwarp textField的文本。即使它出現了相同的錯誤,也可以使用 – Apurva