POST 422(不可处理的实体)Laravel - Ajax

时间:2017-01-27 04:29:19

标签: php jquery angularjs ajax laravel-5

我正在使用Laravel 5.3,我还想说我在Laravel上工作的经验并不多。

我只是想用

构建一个登录表单
  • Laravel
  • Angular JS 1.5
  • Angular Material

并在发布Ajax请求时收到错误

  

POST 422(不可处理的实体)

我也搜索了SO和谷歌,但没有得到任何解决方案。 以下是文件和代码:

的login.php

<?php echo Form::open(array('name'=>'loginForm','autocomplete'=>'off')); ?>
    <md-input-container class="md-block">
        <?php echo Form::label('username', 'Username');  echo Form::text('username', '', 
           array('required'=>'required','ng-model'=>'userD.user.username','md-no-asterisk')); ?>
        <div ng-messages="loginForm.username.$error" role="alert">
            <div ng-message="required">Username is required</div>
        </div>
     </md-input-container>
     <md-input-container class="md-block">
        <?php echo Form::label('password', 'Password');  echo Form::password('password', 
        array('required'=>'required','ng-model'=>'userD.user.password','md-no-asterisk')); ?>
        <div ng-messages="loginForm.password.$error" role="alert">
            <div ng-message="required">Password can not be blank</div>
        </div>
     </md-input-container>
     <md-button type="submit" class="md-primary md-raised" ng-disabled="!loginForm.$valid" 
     ng-click="userD.login()">Login</md-button>
<?php echo Form::close(); ?>

app.js

function login() {
    $http.post('/admin/login', {user: userD.user})
    .success(function (res) {
        $window.location.href = '/admin/dashboard';
    })
    .error(function(res) {
        console.log(res);
        userD.error = res;
    });
}

的LoginController

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class LoginController extends Controller{
  public function index(){
    if(Auth::check() && Auth::user()->UserRoles->role >= 2){
        return Redirect::to('/admin/dashboard');
    }
    return View('admin.login.index');
  }

  public function login(Request $request){
    $data = $request['user'];
    $email = isset($data['username']) ? $data['username'] : null;
    $password = isset($data['password']) ? $data['password'] : null;
    //$remember = isset($data['remember']) ? $data['remember'] : null;
    if(Auth::attempt(['email' => $email, 'password' => $password]/*, $remember*/))
    {
        if(Auth::user()->UserRoles->role > 1)
        {
            return response()->json(["message" => "Welcome"]);
        }
        return response()->json(["The email or password you entered is not correct"], 422);
    }
    return response()->json(["2 : The email or password you entered is not correct"], 422);
  }

}

enter image description here

请不要将此问题标记为重复,因为我尝试了许多仅来自SO的解决方案

0 个答案:

没有答案