laravel with ionic :500 (Internal Server Error)

穿精又带淫゛_ 提交于 2019-12-24 06:15:28

问题


is there anything wrong with my following code? caz it always shows: 500 (Internal Server Error) I do not know why and hope anyone can help,thanks...

I have searched for a day but without any solution. Am i missing something? And i am using ionic to do the mobile application.

//controller

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

Many thanks!


回答1:


Which version of Laravel are you using? If you turn on debugging, what is the message given for the 500 error?

My guess would be that the 'location' index of your request variable is not set. Laravel provides a much better way of retrieving the request input, which already accounts for indexes which may or may not be set, so you should use that.

Anyway, assuming you're on Laravel 5+, do this in your controller:

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

And the same thing, but for 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'));
}


来源:https://stackoverflow.com/questions/36131884/laravel-with-ionic-500-internal-server-error

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