2016-07-30 57 views
0

我在Codeigniter中創建了RESTapi,迄今爲止工作正常。但是今天,當我通過角度調用API時,返回的數據是完全混淆的。我不是第一次使用角度,但現在我無法理解我犯的錯誤。 我的結論是問題是角度的,因爲當我發送「郵遞員」的請求時,結果就是我所期望的。

我的角度控制器是這樣的:

.controller('adminCtrl', function($http, $q, NgTableParams) { 
 
    var vm = this; 
 
    var deferred = $q.defer(); 
 
    vm.selected = {}; 
 

 
    sendHttpRequest = function(req) { 
 
    $http(req).success(function successCallback(response, status) { 
 
     deferred.resolve(response);   
 
     }) 
 
     .error(function errorCallback(error) { 
 
     deferred.reject(error); 
 
     }); 
 
    return deferred.promise; 
 
    }; 
 

 
    vm.editGame = function(game) { 
 
    vm.selected = angular.copy(game); 
 
    }; 
 

 
    vm.reset = function() { 
 
    vm.selected = {}; 
 
    }; 
 

 
    vm.getTemplate = function(game) { 
 
    if (game.gameId === vm.selected.gameId){ 
 
    return 'edit'; 
 
    } 
 
    else return 'display'; 
 
    }; 
 

 
    vm.save = function(idx) { 
 
    
 
    var updateData = { 
 
     method: 'POST', 
 
     url: "<?php echo base_url('api/updateResult'); ?>", 
 
     headers: { 
 
     'Content-Type': 'application/x-www-form-urlencoded', 
 
     'X-API-KEY': "<?php echo $userInfo['API_Key']; ?>" 
 
     }, 
 
     data: { 
 
     'gameId': vm.selected.gameId, 
 
     'homeSaves': vm.selected.homeSaves, 
 
     'awaySaves': vm.selected.awaySaves 
 
     } 
 
    }; 
 
    console.log(updateData); 
 
    sendHttpRequest(updateData).then(function(x) { 
 
     console.log(x); 
 
     vm.games[idx] = angular.copy(vm.selected); 
 
     vm.reset(); 
 
    }, function(error) { 
 
     swal({ 
 
     title: "Error!", 
 
     text: error, 
 
     type: "error", 
 
     confirmButtonText: "OK!", 
 
     closeOnConfirm: true 
 
     }, function() { 
 
     vm.reset(); 
 
     //window.location.href = "<?php echo base_url(); ?>"; 
 
     }); 
 
    }); 
 
    }; 
 
    
 
    
 
    var getData = { 
 
    method: 'GET', 
 
    url: "<?php echo base_url('api/result'); ?>", 
 
    headers: { 
 
     'X-API-KEY': "<?php echo $userInfo['API_Key']; ?>" 
 
    } 
 
    }; 
 

 
    sendHttpRequest(getData).then(function(data) { 
 
    _.forEach(data, function(o) { 
 
     o.homeSaves = parseInt(o.homeSaves); 
 
     o.awaySaves = parseInt(o.awaySaves); 
 
    }); 
 
    vm.games = data; 
 
    }, function(error) { 
 
    swal({ 
 
     title: "Error!", 
 
     text: error, 
 
     type: "error", 
 
     confirmButtonText: "OK!", 
 
     closeOnConfirm: true 
 
    }, function() { 
 
     window.location.href = "<?php echo base_url(); ?>"; 
 
    }); 
 
    }); 
 

 

 
});

它是一些體育比賽結果行內編輯器。 當APP啓動時,我們調用sendHttpRequest(getData),這很好。如果我試圖通過在請求中更改url來模擬錯誤,則將返回錯誤函數,並且「swal」將顯示錯誤。

問題出在vm.save函數中。如果我把它稱爲白所有正確的參數,數據將被保存在我的數據庫中,沒有錯誤。但是,如果我嘗試通過更改url或強制返回錯誤來模擬錯誤,則RESTapi控制器返回的結果令人困惑。
而不是函數(錯誤),函數(x)將被執行並打印完全錯誤的結果(vm.games的結果)甚至REST返回405方法不允許在例子中,當我更改url爲錯誤或400/500當我強制REST控制器返回錯誤。

+0

嘗試把'var deferred = $ q.defer();'sendHttpRequest'函數內?或者也許你總是使用第一個請求已經解決的承諾。 – MMhunter

+0

put ** var deferred = $ q.defer(); ** ** sendHttpRequest **解決問題:) thnx man。我怎麼能upvote你? – klaun

+0

複製粘貼我的答案:D – MMhunter

回答

0

試着把var deferred = $ q.defer();裏面有sendHttpRequest函數。

或者u總是使用第一個請求已解決的承諾。