2016-11-18 56 views
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() 
+0

更换url请使用'$ _POST'代替'$ _REQUEST',只是一个安全提示;其次,你可以在你的php文件中记录'$ _POST':'file_put_contents('log.txt',json_encode($ _ POST,JSON_PRETTY_PRINT));'给我们你得到的东西 – Blag

回答

0

我想你错过了这个

let task = session.dataTask(with: request) {data, response, error in 

request