我想知道,我们如何使用 php 将 MYSQL 数据编码为 JSONP 数据。我们还包括 JSONP 类。请查看以下用于创建 JSONP 的代码。
<?php
require('JSON.php');
$link = mysql_pconnect("localhost", "root", "") or die("Unable To Connect To Database Server");
mysql_select_db("users") or die("Unable To Connect To Northwind");
// add the header line to specify that the content type is JSON
// determine the request type
header("Content-type: application/json");
$sth = mysql_query("SELECT ID, Title FROM ldr");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$data[] = $r;
}
//print json_encode($rows);
$encoder = new Services_JSON();
$json = $encoder->encode($data);
echo 'callback(' . $json . ');';
?>
请查看制作上述代码的确切值,它正在返回这些值。(octave-global.com/portal/tool/index.php)
实际上,我们需要为基于 Kendo-UI 的程序获取这些值,如下所述。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/jquery.min.js"></script>
<script src="js/kendo.all.js"></script>
<link href="css/kendo.common.css" rel="stylesheet" />
<link href="css/kendo.default.css" rel="stylesheet" />
</head>
<body>
<div id="example" class="k-content">
<div id="grid"></div>
<script>
$(document).ready(function () {
dataSource = new kendo.data.DataSource({
transport: {
read:{
url: "http://www.octave-global.com/portal/tool/",
dataType: "jsonp"
},
update: {
url: "data/update.php",
dataType: "jsonp"
},
destroy: {
url:"data/update.php",
dataType: "jsonp"
},
create: {
url: "data/save.php",
dataType: "jsonp"
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
batch: true,
pageSize: 10,
schema: {
model: {
model: {
id: "ID",
fields: {
Title: { editable: true},
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
navigatable: true,
pageable: true,
height: 400,
toolbar: ["create", "save", "cancel"],
columns: [
"Title",
{ command: "destroy", title: " ", width: "110px" }],
editable: true
});
});
</script>
</div>
</body>
</html>
当我们阅读此 URL 时,它不起作用:- octave-global.com/portal/tool/ 它仅适用于此 URL:- demos.kendoui.com/service/Products。
请建议我如何根据此 URL demos.kendoui.com/service/Products 进行 PHP 配置。
谢谢
竿