angularjs $http request getting called multiple times

和自甴很熟 提交于 2019-12-25 14:08:57

问题


i am making simple http request to doGet method of servlet to fetch data from table. bellow is my code.

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 method

    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");        
}

Whenever i click to fetch data it never get me new data once i change the table name in tabname="saleq315"; after multiple click table data refresh. and when i log this to console i see http request getting executed mulpliple times. If you see the image attached, i changed the table name and click to get the data,

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]

来源:https://stackoverflow.com/questions/32452810/angularjs-http-request-getting-called-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!