2012-03-09 40 views
0

我想知道,我們如何使用php將MYSQL數據編碼成JSONP數據。我們還包括JSONP類。請看下面的代碼來創建JSONP。我們怎樣才能通過PHP將Mysql數據編碼成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> 

它不工作,當我們讀到這個網址: - octave-global.com/portal/tool/ 這只是這個URL工作: - demos.kendoui.c​​om/service/Products。

請建議我如何根據這個URL做出我的PHP配置demos.kendoui.c​​om/service/Products。

感謝

ROD

回答

0

語法錯誤之外,您需要將您的內容類型切換到應用程序/ JavaScript的應用程序/ x-的JavaScript。您的JSONP請求(通過Kendo UI)由JavaScript解釋器進行評估。它們不被JSON解析器解析。