PHP-PDO:如何将一些返回值包装到javascript中,以便在视图上呈现?
我是MVC新手,我正在尝试从mysql数据库中检索一组图像路径,并用它通过javascript构建一个网格 控制器需要一个调用数据库并返回所有路径的模型 以前,我有一个ajax调用打开了一个设计糟糕的文件,该文件PHP-PDO:如何将一些返回值包装到javascript中,以便在视图上呈现?,javascript,php,mysql,ajax,pdo,Javascript,Php,Mysql,Ajax,Pdo,我是MVC新手,我正在尝试从mysql数据库中检索一组图像路径,并用它通过javascript构建一个网格 控制器需要一个调用数据库并返回所有路径的模型 以前,我有一个ajax调用打开了一个设计糟糕的文件,该文件echo json_encode() 目前,所有文件都通过index.php路由。那么,我是否会在控制器中echo json_code(),并打开ajaxindex.php model/interest.php class Interest { private $cxn = Dd::
echo json_encode()
目前,所有文件都通过index.php
路由。那么,我是否会在控制器中echo json_code()
,并打开ajaxindex.php
model/interest.php
class Interest
{
private $cxn = Dd::getInstance();
private $table = 'interestPix'
function __construct(){}
public function get_interest_pix()
{
$sql = "SELECT * FROM $this->table";
if ($stmt = $this->cxn->prepare($sql)) {
$stmt->execute();
$result = $stmt->fetchAll();
$stmt = null;
$this->$cxn = null;
return $result;
}
}
controller/interest\u controller.php
require_once 'models/interest.php';
/**
*
*/
class InterestController
{
function __construct(argument){}
public function render_interest_pix()
{
$model = new Interest();
}
}
ajax文件:
function interest_grid() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonObj = xmlhttp.response;
myFunc(jsonObj);
}
};
xmlhttp.open("GET","views/interest_controller.php",true);
xmlhttp.responseType = "json";
xmlhttp.send();
}
本质上,是的,您将让控制器回显结果的json_编码。确保所有这些都得到了响应,否则在解析结果时可能会出现javascript错误