angularjs $ http请求被多次调用

时间:2015-09-08 08:26:04

标签: javascript angularjs servlets

enter image description here我正在对servlet的doGet方法进行简单的http请求以从表中获取数据。吼叫是我的代码。

var myApp = angular.module('myApp', []);

myApp.controller('myController', function ($scope,$http) {


$scope.getDataFromServer = function() { 

            $http    
({         
    method:'GET',
    url:'jasoncontroller'    
}).success(function(data, status, headers, config){ 
                $scope.sqldata = data;                    

                console.log("executing");
                $scope.$watch('sqldata',function(newValue,oldValue){
                    console.info('changed');
                    console.log('new : ' + newValue);
                    console.log('old : ' + oldValue);
                    console.log('watch end');                    
                });            


}).error(function(data, status, headers, config){});

    };   

});

servlet doGet方法

    protected void doGet(HttpServletRequest request, HttpServletResponse           response) throws ServletException, IOException {     
       System.out.println("Executing get");     
    getSqlData sql = new getSqlData();          
    Gson gson = new GsonBuilder().serializeNulls().create();        
    String tabname="saleq315";                      
    String json = gson.toJson(sql.gettabledata(tabname));       
    response.setContentType("application/json");
    response.getWriter().write(json);
    System.out.println("data sent");        
}

每当我点击获取数据时,一旦我更改了tabname =" saleq315"中的表名,我就永远不会得到新数据。多次单击表数据刷新后。 当我将其记录到控制台时,我看到http请求被执行mulpliple次。 如果您看到附加的图像,我更改了表名并单击以获取数据,

first click - same table data --> console log one entry
seconds click - same table data --> console log one entry
third click - got new data --> console log 5 entries.
 Not sure what is happening here.

[![enter image description here][1]][1]

0 个答案:

没有答案