2015-10-23 69 views
0

的jsfiddle jQuery的AJAX功能,我有內點擊數 不工作的onLoad

  • 內的jsfiddle

    • 它的工作原理的jQuery的AJAX功能,它不會在最初的頁面加載工作。

    這是可能的還是我錯過了什麼?

    這裏是的jsfiddle:

    http://jsfiddle.net/djlerman/bbj8k9pe/

    我試圖延遲選項,但沒有解決問題。

    // Run ajax function onLoad -- DOESN'T WORK 
     
    getNodeViaAjax(); 
     
    
     
    // Run ajax function ON Button click -- WORKS 
     
    $("#buttonID").on("click", "", function() { 
     
        getNodeViaAjax(); 
     
    }); 
     
    
     
    // ajax function 
     
    function getNodeViaAjax() { 
     
        $.ajax({ 
     
    \t \t type: 'POST', 
     
         url: '/echo/json/', 
     
         data: { 
     
          json: JSON.stringify(jsonData) 
     
         }, 
     
         success: function(data) { 
     
          $('#displayResponse').html(JSON.stringify(data)); 
     
         }, 
     
         error:function(error){ 
     
          alert('there was an error'); 
     
         }, 
     
         dataType: 'json' 
     
        }); 
     
    } 
     
    
     
    
     
    // Data to return via ajax 
     
    /* This is an echo of some data sent back via ajax      */ 
     
    /* This data should be filtered by nodeID and return only childNodeID's. */ 
     
    /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv. */ 
     
    var jsonData = { 
     
        "nodeID": { 
     
        "1": { 
     
         "childNodeID": { 
     
         "1.1": { 
     
          "childNodeType": "branch", 
     
          "childData": [ 
     
          "1.1: column 1", 
     
          "1.1: column 2" 
     
          ] 
     
         }, 
     
         "1.2": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "1.2: column 1", 
     
          "1.2: column 2" 
     
          ] 
     
         }, 
     
         "1.3": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "1.3: column 1", 
     
          "1.3: column 2" 
     
          ] 
     
         } 
     
         } 
     
        }, 
     
        "1.1": { 
     
         "childNodeID": { 
     
         "1.1.1": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "1.1.1: column 1", 
     
          "1.1.1: column 2" 
     
          ] 
     
         }, 
     
         "1.1.2": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "1.1.2: column 1", 
     
          "1.1.2: column 2" 
     
          ] 
     
         } 
     
         } 
     
        }, 
     
        "2": { 
     
         "childNodeID": { 
     
         "2.1": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "2.1: column 1", 
     
          "2.1: column 2" 
     
          ] 
     
         }, 
     
         "2.2": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "2.2: column 1", 
     
          "2.2: column 2" 
     
          ] 
     
         }, 
     
         "2.3": { 
     
          "childNodeType": "leaf", 
     
          "childData": [ 
     
          "2.3: column 1", 
     
          "2.3: column 2" 
     
          ] 
     
         } 
     
         } 
     
        } 
     
        } 
     
    }; 
     
    /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ 
     
    /* This is an echo of some data sent back via ajax      */ 
     
    /* This data should be filtered by nodeID and return only childNodeID's. */
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
     
    <button id="buttonID">Click to Load ajax</button> 
     
    <div id="displayResponse"></div>

  • +0

    http://jsfiddle.net/bbj8k9pe/4/ – guest271314

    回答

    4

    如果添加一個快速console.log聲明,你會發現你的jsonData是不確定的...

    function getNodeViaAjax() { 
        console.log('Running now...' + jsonData); 
    

    您正在嘗試發送的POST數據,所以請確保數據在使用之前已初始化...雖然變量已懸掛,但值的設置位置不是 - 請將您的var jsonData = ...移至之前0函數調用。

    +0

    就是這樣。謝謝! jsonData需要在getNodeViaAjax()被初始調用之前被定義。 –