2016-06-09 78 views
0

我想用Angular進行登錄,但只要我提交表單,我在控制檯POST http://localhost/angulav/public/auth 500(內部服務器錯誤)。角路由錯誤 - POST http http:// localhost/angulav/public/auth 500(內部服務器錯誤)

這是我的路線

Route::any('/', function() { 
    return view('layout.master'); 
    })->where('path', '.+'); 

Route::post('/auth', '[email protected]'); 

這是我的UserController中,JS

angular.module('myApp').controller('userController', ['$scope', '$http', function($scope, $http){ 
angular.extend($scope, { 
    doLogin: function(loginForm) { 
    $http({ 
     headers: {'Content-Type': 'application/json'}, 
     url: 'auth', 
     method: "POST", 
      data: { 
      username: $scope.login.username, 
     password: $scope.login.password 
      } 
    }).success(function(response){ 
     console.log(response); 
      }); 
      } 
}); 

這是我的HTML

<form class="" ng-submit="doLogin(loginForm)"> 
     <div class="form-group"> 
     <label for="username">Username:</label> 
     <input id="username" class="form-control" ng-model="login.username" type="text" name="username" placeholder="Enter username"> 
     </div> 
    <div class="form-group"> 
     <label for="password">Password:</label> 
     <input id="password" class="form-control" ng-model="login.password" type="password" name="password" placeholder="Enter username"> 
     </div> 
     <div class="form-group"> 
      <input class="btn btn-primary pull-right" id="username" type="submit" value="Submit"> 
     </div> 
</form> 

這是我usercontroller.php

public function login(Request $request) 
    { 
    $user_data = [ 
    'username' => $request->input('username'), 
    'password' => $request->password('password') 
    ]; 

    if (Auth::attempt($user_data)) { 
     return response(Auth::user(), 201); 
    } else { 
     return response('username and password do not match', 403); 
    } 
} 
+0

你有沒有試穿郵差? –

+0

@PareshGami:請問郵差是什麼? – nana

+0

郵差是從哪裏可以測試你的API,因爲開始發展的工具。只需在谷歌郵遞員搜索鉻 –

回答

0

你的內容類型是json,它是一個但你通過一個對象發送。 如果你有

data: { 
     username: $scope.login.username, 
     password: $scope.login.password 
    } 

將其更改爲

data: JSON.stringify({ 
     username: $scope.login.username, 
     password: $scope.login.password 
    }) 

如果還是不行,請嘗試更改標題的內容類型信息,以application/x-www-form-urlencoded而不是application/json,然後分析你的數據對象到$ httpParamSerializer功能,見下文。 (不要忘記注入它)。

data: $httpParamSerializer({ 
     username: $scope.login.username, 
     password: $scope.login.password 
    }) 
相關問題