1
我試圖從用戶發送輸入到一個php腳本,讓它最終傳遞給一個SQL服務器,大多數的代碼運行但有提交數據的問題。swift 3 http請求
@IBAction func submit(_ sender: AnyObject) {
let requestURL = URL(string: "*****")
let request = NSMutableURLRequest(url:requestURL!)
request.httpMethod = "POST"
let song=txt1.text!
let artist=txt2.text!
let album=txt3.text!
let year=txt4.text!
let genre=txt5.text!
let songPost = "song=" + (song as String)
let artistPost = "&artist=" + (artist as String)
let albumPost = "&album=" + (album as String)
let yearPost = "&year=" + (year as String)
let genrePost = "&genre=" + (genre as String)
request.httpBody = songPost.data(using: String.Encoding.utf8);
request.httpBody = artistPost.data(using: String.Encoding.utf8);
request.httpBody = albumPost.data(using: String.Encoding.utf8);
request.httpBody = yearPost.data(using: String.Encoding.utf8);
request.httpBody = genrePost.data(using: String.Encoding.utf8);
--->>let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
print(response)
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
存在urlsession.shared.datatask代碼行問題。編譯器錯誤說「ambigious引用member'dataTask(with:completionhandler :)」
什麼可以讓此代碼工作,以及如何驗證此信息是通過應用程序?
那真棒!我想我正在用swift 2找到答案,並將其混合到我的代碼中,我無法將它們分開。它不顯示任何錯誤和運行,但是當我點擊提交應用程序時,有一個線程1:斷點1.1錯誤,其綠色也不是紅色,它使用var request = URLREQUEST在線上正確使用。什麼是斷點? – xteetsx
也即時得到一個錯誤,說無效重新宣佈的'viewController'出現的地方,我沒有任何具有相同名稱的功能 – xteetsx
好的答案。你也可以用'.utf8'替換所有'String.Encoding.utf8'引用。 – Rob