2013-09-24 40 views
0

我需要通過從數據庫中提取來在我的頁面上顯示評論。從服務器獲取數據並顯示

我能夠從.json文件中獲得虛擬測試數據,但是當我嘗試將它與數據庫url進行連接時,它並未獲取數據。

我使用js handlebar模板來循環html頁面中的數據。

這裏是我的js來fecth數據

var getData = function() { 
     console.group("getData()", "Fetching data from server at", R.settings.endPoints.incoming.getData); 

     var promise = $.ajax({ 
     url: R.settings.endPoints.incoming.getData 
     }); 

     console.log("getData()", "Returing the promise", { promise: promise }); 

     console.groupEnd(); 

     return promise; 
    }; 

DB URL設置與.getdata像下面

endPoints: { 
      incoming: { 
      getData: "http://localhost:8080/rest/review/getReview" 
      }, 
      outgoing: { 
      sendData: "http://localhost:8080/rest/review/createReview" 
      } 
     } 

回答

0

jQuery的$.ajax功能在默認情況下異步的,所以你需要把你的處理/將代碼顯示爲deferred.done方法如下:

var promise; 

$.ajax({ 
    url: R.settings.endPoints.incoming.getData 
}) 
    .done(function(msg) { 
    console.log(msg); 
    promise = msg; 
    }); 

但是,如果要執行同步請求,做這樣的事情:

var promise = $.ajax({ 
     url: R.settings.endPoints.incoming.getData, 
     async: false 
     }); 

注意的是,根據官方文檔:

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; 
you must use the success/error/complete callback options instead of the 
corresponding methods of the jqXHR object such as jqXHR.done() or the 
deprecated jqXHR.success(). 
相關問題