2014-02-25 70 views
2

的Javascript:我的Ajax調用不工作 - 嘗試通過AJAX PHP和MySQL JSON填充jstree

$('#jstree1').jstree({ 'core' : { 
     // I usually configure the plugin that handles the data first 
     // This example uses JSON as it is most common 
     "json_data" : { 
      // This tree is ajax enabled - as this is most common, and maybe a bit more complex 
      // All the options are almost the same as jQuery's AJAX (read the docs) 
      "ajax" : { 
       // the URL to fetch the data 
       "type" : "POST", 
       "url" : "./ajax/get_page_data.php", 
       "dataType": "JSON", 
       "contentType": "application/json;", 
       "data": 
       "d_id="+<?=$DOC['d_id']?>, 
       "success" : function (data) { 
        // 'data' is a JSON object which we can access directly. 
        // Evaluate the data.success member and do something appropriate... 
        alert(data); 
        if (data.success == true){ 
         $('#section1').html(data); 
        } else { 
         $('#section2').html(data); 
        } 
       }, 
       "error": function (error) { 
         alert('error; ' + eval(error)); 
       }    
      } 
     } 
} }); 

及以下爲get_page_data.php它不是相當的工作我的輸出:

[{"id":"ajson12","parent":"#","text":"Welcome"},{"id":"ajson13","parent":"#","text":"Getting to Us"},{"id":"ajson14","parent":"13","text":"About Us"},{"id":"ajson15","parent":"13","text":"Visit Us"},{"id":"ajson16","parent":"13","text":"Bus Routes"},{"id":"ajson17","parent":"#","text":"Choices"},{"id":"ajson18","parent":"#","text":"Guidance"},{"id":"ajson19","parent":"#","text":"Facilities"}] 

在測試一些JSON數據手動下面將工作:

$('#jstree1').jstree({ 'core' : { 
    'data' : [{"id":"ajson12","parent":"#","text":"Welcome"},{"id":"ajson13","parent":"#","text":"Getting to Us"},{"id":"ajson14","parent":"13","text":"About Us"},{"id":"ajson15","parent":"13","text":"Visit Us"},{"id":"ajson16","parent":"13","text":"Bus Routes"},{"id":"ajson17","parent":"#","text":"Choices"},{"id":"ajson18","parent":"#","text":"Guidance"},{"id":"ajson19","parent":"#","text":"Facilities"}] 
} }); 

我到處都找過網,不能找到一個完美的例子,通過使用PHP array/mysql提供並輸出爲Json的ajax來填充jsTree。

+0

您好,我刪除了您的帖子中的鏈接,因爲它很脆弱,請立即將您的mysql *函數更改爲PDO(或)Mysqli函數。 –

+0

[PHP 4?它的支持已經在2008年停止。](http://www.php.net/archive/2008.php#id2008-08-07-1) – Gumbo

+0

對不起,它更像是PHP 5.2或者我只是認爲它是4. –

回答

3

試試這個:

$('#jstree1').jstree({ 
      'core': { 
       'data': { 
        'url': "./ajax/get_page_data.php", 
        'type': 'POST', 
        'dataType': 'JSON', 
        'contentType':'application/json', 
        'data': function (node) { 
         return { 'd_id': <?=$DOC['d_id']?>}; 
        } 
       } 
      } 
     }); 

我不知道,你可以自定義聯播和.success功能.error對jstree。

+0

我收到以下錯誤未捕獲SyntaxError:156行上的意外字符串 –

+0

這可以手動運行\t $('#jstree1')。jstree({'core':{ \t \t'data':[{「id」:「ajson12 「,」parent「:」#「,」text「:」Welcome「},{」id「:」ajson13「,」parent「:」#「,」text「 「:」 ajson14" , 「父」: 「ajson13」,「TEX t「:」關於我們「},{」id「:」ajson15「,」parent「:」ajson13「,」text「:」Visit Us「},{」id「:」ajson16「,」parent「 ajson13「,」text「,」公交路線「},{」id「:」ajson17「,」parent「:」#「,」text「:」選擇「},{」id「:」ajson18「,」 「:」#「,」text「:」Guidance「},{」id「:」ajson19「,」parent「:」#「,」text「:」Facilities「}] \t}}); –

+0

@zaitman當我刪除行'contentType'它的作品! –