在Xcode中,我目前收到以下错误:
错误域= NSCocoaErrorDomain代码= 3840"垃圾结束。" UserInfo = {NSDebugDescription =结尾处的垃圾。}
当我在我的php脚本中添加一个函数来检索' community'数据库中的详细信息。这些细节存储在一个数组中。
以下行echo json_encode($communities);
似乎导致问题,因为没有它就可以正常运行。下面是完整的userLogin.php脚本。那条线位于底部。
<?php
require ("Conn.php");
require ("MySQLDao.php");
$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$returnValue = array();
if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}
$secure_password = md5($password);
$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetailsWithPassword($email,$secure_password);
if(!empty($userDetails))
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($userDetails);
}else{
$returnValue["status"] = "error";
$returnValue["message"] = "User is not found";
echo json_encode($returnValue);
}
//once logged in run function to get list of communities
$communities = array();
$communities = $dao->getCommunities($email);
echo json_encode($communities);
$dao -> closeConnection();
?>
我在浏览器中测试了SQL函数,它返回正确的值,输出如下:
[{&#34;名称&#34;:&#34; EnclliffeT&#34;},{&#34;名称&#34;:&#34; OneWinner&#34;},{&#34;名称& #34;:&#34; YESSS&#34;},{&#34;名称&#34;:&#34; Treert&#34;},{&#34;名称&#34;:&#34;西布&# 34;}]
我很确定问题是Swift没有正确接收数组。
这是用户登录时运行的Swift代码,它提供错误:
@IBAction func loginButtonTapped(_ sender: AnyObject)
{
let userEmail = userEmailTextField.text;
let userPassword = userPasswordTextField.text;
if (userPassword!.isEmpty || userEmail!.isEmpty) { return; }
// send user data to server side
let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/userLogin.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
let postString = "email=\(userEmail!)&password=\(userPassword!)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
if(error != nil)
{
//Display an alert message
let myAlert = UIAlertController(title: "Alert", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
myAlert.addAction(okAction);
self.present(myAlert, animated: true, completion: nil)
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
// retrieve login details and check to see if all ok
if let parseJSON = json {
let returnValue = parseJSON["status"] as? String
if(returnValue != "error")
{
self.delegate?.userLoggedIn(data: userEmail! )
UserDefaults.set(UserDefaults.standard)(true, forKey: "isUserLoggedIn");
self.dismiss(animated: true, completion: nil)
} else {
// display an alert message
let userMessage = parseJSON["message"] as? String
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil)
myAlert.addAction(okAction);
self.present(myAlert, animated: true, completion: nil)
}
}
} catch
{
print(error)
}
}
}
task.resume()
}
答案 0 :(得分:1)
您正在输出多个json块,这是非法的json:
if(!empty($userDetails))
...
echo json_encode(...)
} else {
...
echo json_encode(...)
}
...
echo json_encode(...)
JSON文本块只能包含一个 SINGLE json结构。由于你有两个,你有一个JSON语法错误。
e.g。
echo json_encode('hi');
echo json_encode('mom');
产生
"hi""mom"
由于JSON IS javascript代码,您基本上是在尝试这样做:
var foo = "hi""mom";
^--syntax error, unexpected string