laravel with ionic:500(内部服务器错误)

时间:2016-03-21 13:07:22

标签: php angularjs laravel ionic-framework

以下代码有什么问题吗?因为它总是显示: 500(内部服务器错误) 我不知道为什么,希望有人能提供帮助,谢谢......

我搜索了一天但没有任何解决方案。我错过了什么吗? 我正在使用离子来做移动应用程序。

//控制器

 public function touristsData(){//get
   $postdata = file_get_contents("php://input");
   $request = json_decode($postdata,true);
   $location = $request['location'];
   if(empty($location) != true){
           $tourists = User::where('block','0')
                       ->where('location', $location)
                       ->orderBy('updated_at', 'desc')
                       ->get();                 
   }else{
           $tourists = User::where('block','0')
                       ->orderBy('updated_at', 'desc')
                       ->get();         
   }
   return View::make('frontend.data.touristsData',array('tourists'=>$tourists));
 }

// app.js(angularjs)

     $scope.submitForm = function(){
    if($scope.filter.want == 'company'){
          $http({
              method  : 'POST',
              url     : './touristsData',
              beforeSend: function (xhr) {
                          var token = document.getElementById('token').getAttribute('content');                              
                          if (token) {
                                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                          }
              },                  
              data    : {
                  'location': $scope.filter.location
              },
              headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
          })
          .success(function(data){
               $scope.tourists = data;
          });    
          $scope.modal.hide();                   
    }
  }

非常感谢!

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的Laravel?如果打开调试,500错误的消息是什么?

我的猜测是你的请求变量的'location'索引没有设置。 Laravel提供了一种更好的retrieving the request input方法,它已经考虑了可能设置或未设置的索引,所以你应该使用它。

无论如何,假设您使用的是Laravel 5+,请在您的控制器中执行此操作:

public function touristsData()
{
    $query = User::where('block','0');

    if (request()->has('location')) {
        $query->where('location', request('location'));
    }

    $tourists = $query->orderBy('updated_at', 'desc')->get();

    return view('frontend.data.touristsData', compact('tourists'));
}

同样的事情,但是对于Laravel 4.2:

public function touristsData()
{
    $query = User::where('block','0');

    if (Input::has('location')) {
        $query->where('location', Input::get('location'));
    }

    $tourists = $query->orderBy('updated_at', 'desc')->get();

    return View::make('frontend.data.touristsData', compact('tourists'));
}