1
我試圖讓登記swift3與MySQL 但有一些東西錯了, 當我運行我的應用程序,使寄存器返回消息「有些字段爲空」SWIFT 3與MySQL
在PHP代碼:
$fullname = htmlentities($_REQUEST["fullname"]);
$username = htmlentities($_REQUEST["username"]);
$email = htmlentities($_REQUEST["email"]);
$password = htmlentities($_REQUEST["password"]);
$phone = htmlentities($_REQUEST["phone"])
SWIFT代碼:
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: "http://localhost/php/register.php?")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let body = "fullname=\(FirstName.text)%20\(LastName.text)&username=\(UserName.text)&email=\(Email.text)&password=\(Password.text)&phone=\(Phone.text)"
request.httpBody = body.data(using: String.Encoding.utf8)
let task = session.dataTask(with: url!) {data, response, error in
if error != nil {
print(error!.localizedDescription)
} else {
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
{
print(json)
}
} catch {
print("error in JSONSerialization")
}
}
}
task.resume()
更換
url
請使用'$ _POST'代替'$ _REQUEST',只是一個安全提示;其次,你可以在你的php文件中記錄'$ _POST':'file_put_contents('log.txt',json_encode($ _ POST,JSON_PRETTY_PRINT));'給我們你得到的東西 – Blag