当我的脚本要求服务器加载数据并接收所需的数据时,一切都会顺利进行,但是当找不到所需的数据时,我将无法执行其他操作。
func myJSON(sec:Int) {
var request = URLRequest(url: URL(string: "https://xx.com/index.php")!)
let postString = "sec=\(sec)"
request.httpMethod = "POST"
request.httpBody = postString.data(using: .utf8)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
if error == nil, let data = data {
do {
if let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:String]] {
for item in jsonData {
if let sec = item["sec"] {
self.myArray_sec.append(sec)
DispatchQueue.main.async {
self.myTable!.reloadData()
//It is happening here and things are going well
self.myTable!.tableFooterView?.isHidden = true
}
}else if let error = item["error"]{
DispatchQueue.main.async {
print("error: ",error)
//But here it cannot be done
self.myTable!.tableFooterView?.isHidden = true
}
}
}
}
} catch let error as NSError {
print("error: ",error)
}
}
}
task.resume()
}
打印错误:无数据
PHP代码有效
$result = mysqli_query($con,"SELECT * FROM `myTable` WHERE `sec`='".$_POST['sec']."';");
while($data = mysqli_fetch_assoc($result)) {
$rows[] = $data;
}
if ($rows[0]['sec'] != "") {
print json_encode($rows);
}
else {
print '[{"error":"no Data"}]';
}
答案 0 :(得分:0)
问题已由计时器解决
}else if let error = item["error"]{
DispatchQueue.main.async {
print("error: ",error)
let myTimer = Timer.scheduledTimer(timeInterval: 2,
target: self,
selector: #selector(self.refresh),
userInfo: nil,
repeats: false)
}
}
@objc func refresh() {
self.myTable!.tableFooterView?.isHidden = true
}