2016-06-09 79 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 
    }) 
相关问题