【问题标题】:Why AngularJs doesn't always load data?为什么 AngularJs 并不总是加载数据?
【发布时间】:2017-06-29 11:47:51
【问题描述】:

我正在将数据加载到$scope,但它并不总是加载。我有时会在刷新错误时遇到

GET http://localhost/allClients 500 (Internal Server Error)

有时它会起作用。我不知道为什么会这样。

我在 Angular 中有这样的代码:

$http.get('allClients')
    .then(function(res){
       $scope.clients = res.data;
});

我的服务器端是 Laravel 5.4

编辑: 在 laravel 上查询

public function allClients(){

    $system_id = env('SYSTEM_ID');

    return DB::select("SELECT 
                            client.*
                       FROM 
                            client 
                        WHERE 
                            system_id = $system_id") ;
 }

【问题讨论】:

  • 这是一个服务器端错误,我认为 angular 与此无关..
  • 检查服务器上的错误。它是一个服务器错误。尝试直接在浏览器中点击 url。你仍然应该得到同样的错误。
  • 我注意到 laravl 有时会遇到导致问题的环境变量的问题。我已经编辑了我的问题。

标签: javascript php angularjs laravel-5.4


【解决方案1】:

来自 Angular 的文档:

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

您的 API 调用返回内部服务器错误,因此 $http 运行 errorCallback

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2012-08-10
    • 2013-06-25
    • 2011-04-06
    相关资源
    最近更新 更多