我正在使用插件 ParamQuery 网格,并且数据源需要获取 json 数据,这就是我想用 $. getJson (...) {},但我有一个名为 data.php 的 PHP 类,其中包含一个名为 GetData 的方法,其他 InsertData、DeleteData、(使用 CRUD 的 PDO),GetData 将 infromacion 作为 json 返回。 问题是如何从 jquery 调用函数?
我使用的代码是:
数据.php
<?php
class Data {
private $db = NULL;
const DB_SERVER = "localhost";
const DB_USER = "root";
const DB_PASSWORD = "usbw";
const DB_NAME = "musicstore";
public function __construct() {
$dsn = 'mysql:dbname=' . self::DB_NAME . ';host=' . self::DB_SERVER;
try {
$this->db = new PDO($dsn, self::DB_USER, self::DB_PASSWORD);
} catch (PDOException $e) {
throw new Exception('Connection failed: ' . $e->getMessage());
}
return $this->db;
}
public function getData() {
$statement = $this->db->prepare("Select * from Customer");
$statement->execute();
if ($statement->rowCount() > 0) {
echo json_encode($statement);
}
return false;
}
}
?>
函数jquery.js
$.getJSON('Data.php', function(data) {
var obj = {};
obj.width = 1000;
obj.height = 400;
obj.colModel = [{title: "Rank", width: 150, dataType: "integer"},
{title: "Company", width: 200, dataType: "string"},
{title: "Revenues ($ millions)", width: 200, dataType: "float", align: "right"},
{title: "Profits ($ millions)", width: 200, dataType: "float", align: "right"}];
obj.dataModel = {data: data};
$("#grid_array").pqGrid(obj);
});