1
.factory('ChartService', ['$http','$q', 
     function ChartService($http,$q) { 
      // interface 

      // implementation 
      var canceler = $q.defer(); 

      function getTableData() { 

       return $http.post('http://202.429.115.52:9906/oo/api.php?request=getSubfunctionWiseHCAndSW').success(function (data) { 
        if (data.mm == "No Data Available"){ 
         localData(); 
        } 
        return data; 


       }).error(function(error){ 
        alert("error") 
        canceler.resolve(); 
        localData() 
        // console.log(error) 
       }); 



       } 

      function localData(){ 
         alert("loaddata") 
       return $http.get('vro/hcswc.json').success(function(response){ 
        console.log(response+"=="); 
        return response; 
       }).error(function(error){ 
        console.log(error); 
       }); 
      } 




      return { 
       getTableData:getTableData 
      } 
     } 
    ]); 

錯誤 的XMLHttpRequest無法加載http://192.127.215.52:9906/api.php?request=getSubfunctionrection。請求的資源上沒有「Access-Control-Allow-Origin」標題。因此不允許訪問原產地'http://localhost:63342'。響應有HTTP狀態代碼404如何去除角度js中的404錯誤?

App.js //決心代碼

.state('app.vro', { 
     url: "/vro/:isfirstActiveState", 
     views: { 
      'menuContent': { 
      controller: "vrobCtrl", 
      templateUrl: 'vrob/vrob.html' 


      } 
     } , 
      resolve: { 
       tableData:function(ChartService){ 

        return ChartService.getTableData(); 
       } 



      } 
     }) 

你好 我想獲得的數據形式的服務。我有個條件,同時調用webservice的.the條件如果我會得到錯誤,那麼我從本地獲取請求,換句話說,如果我從服務器得到任何錯誤,我需要從本地讀取json文件。我需要用決心和打電話給我的服務,並使用控制器上的數據。我localData沒有返回的數據來解決。什麼是我的代碼問題..

這裏是一個例子 http://plnkr.co/edit/0y9V0m2hmsUBRXoeyjig?p=preview 我得到錯誤正確的,但它應該載入從本地JSON。爲什麼它不是從本地JSON數據加載數據

+0

看起來你需要讓你正試圖從獲得的數據在服務器上CORS。 服務器上運行什麼語言? –

+0

我只是在閱讀json flle ..但是在角度上我們可以閱讀json文件 – user944513

+0

@ penne12問題與CORS無關......它是如何管理的,如果第一個被拒絕,則返回另一個承諾 – charlietfl

回答

1

它是一種反模式使用$q管理$http請求,但在這種情況下,我不知道另一種方式:

function getTableData() { 

    var deferred = $q.defer(); 

    $http.post('http://202.129.215.52:9906/mondelez/api.php?request=getSubfunctionWiseHCAndSW') 
    .then(
     // resolve callback 
     function(data) { 
     deferred.resolve(data.data) 
     return data.data; 
     }, 
     // reject callback make different request 
     function(err) { 
     $http.get('data.json').success(function(response) { 
      deferred.resolve(response);    
     }).error(function(error) { 
      deferred.reject() 
     }); 

     }) 

    return deferred.promise; 

} 

DEMO

+0

你從哪裏讀到它是反模式?在整個應用程序中只傳遞一種類型的承諾肯定有好處。 – soote

+0

@soote'$ http'已經返回一個承諾http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – charlietfl

+0

The http諾言與q諾言有不同的界面,這意味着你現在需要知道你正在處理的是哪種類型的諾言。 – soote