jeudi 26 janvier 2017

POST 422 (Unprocessable Entity) Laravel - Ajax

I am using Laravel 5.3 and i would also like to say that i am not that much experienced working on Laravel.

I am just trying to build a Login Form with

  • Laravel
  • Angular JS 1.5
  • Angular Material

and getting an error while posting Ajax request as

POST 422 (Unprocessable Entity)

I have searched SO and google as well but not got any solution to this problem. Below are the files and code:

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

Please do not mark this question as duplicate as i have tried many solutions from SO only



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire