猿问

登录后/刚刚刷新页面后未加载数据

我在我的页面上进行了简单的登录。登录后,我正在显示来自服务器的数据,这只是在刷新页面一次后显示。在我收到错误 500 之前:


GET http://localhost:8080/getDesktop 500

angular.js:14800 Possibly unhandled rejection: ...

我的 AngularJS 代码如下:


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


app.controller("myCtrl", function($scope, $http, $window, $timeout){


//Username

$scope.user  = "";


//Desktop

$scope.storage = [];


//Get username


$http.get("/loggeduser", {transformResponse: function(response){

        return JSON.stringify(response);

    }

}).then(function(response) {

    $scope.user = response.data.substring(1, response.data.length-1);

});


//Show Desktop


    $http.get("/getDesktop").then(function(response){

    for(var i = 0; i<response.data.length; i++){

           $scope.storage[i] = {name: response.data[i]};

       }           

       return;

   });

});

后端:


//Returns Desktop

@GetMapping("/getDesktop")

public ArrayList<String> getDesktop() throws Exception {

    ArrayList<String> itemNames = new ArrayList<>();


    if(kdxF.getUser() != null) {

        itemNames = kdxF.showDesktop();  

        // Just a function to return the Elements in an ArrayList if Strings

        // If user is logged in

    }else {

        throw new Exception("Not logged in!");

    }


    return itemNames;

}

我收到消息“未登录”


宝慕林4294392
浏览 197回答 1
1回答

桃花长相依

好的,我用一个丑陋的解决方案解决了它,但它有效。我不断检查用户是否为空,然后加载数据。//Returns Desktop@GetMapping("/getDesktop")public ArrayList<String> getDesktop() throws Exception {&nbsp; &nbsp; ArrayList<String> itemNames = new ArrayList<>();&nbsp; &nbsp; int kill = 0;&nbsp;&nbsp; &nbsp; while(kdxF.getUser() == null) {&nbsp; &nbsp; // FIXME&nbsp; &nbsp; &nbsp; &nbsp; if(kill == 500) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kill++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; itemNames = kdxF.showDesktop();&nbsp; &nbsp; return itemNames;}如果你有更好的建议,请告诉我。:)
随时随地看视频慕课网APP

相关分类

Java
我要回答