【发布时间】:2017-04-02 12:17:27
【问题描述】:
您好,我正在编写一个 ios swift 3 应用程序来与网站通信,该应用程序在做了很多事情后应该返回一个 false 或 true 的类型值,但它不会发生你可以告诉我我在哪里错误以及如何纠正错误!
快速返回值: ....response = 可选({ URL:“http://....myurl.php”}.....
SWIFT 代码:
let myUrl = URL(string: "http://....myurl.php");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "username=James&password=Bond";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)");
// return false
}
print("response = \(response)")
}
task.resume()
return 0;
PHP 代码:
include 'user.php';
$user = new User();
$username= $_REQUEST["username"];
$password = $_REQUEST["password"];
if($user->login($username,$password)==true){
echo json_encode("true");
}
else{
echo json_encode("false");
}
错误图片:
【问题讨论】: