简体   繁体   中英

Ajax to Laravel Returns 404 and Responsejson is undefined

I am get an empty response when I $.ajax to a route that executes the controller below from a cross domain request. When I uncomment the var_dump line I get a response with data otherwise I get a 404 response and the responsejson object is undefined. Any help greatly appreciated. When I access the get equivalent of the same route directly in the browser, I get a valid json response.

<?php

use App\Models\User;

class AuthenticationController extends \BaseController {

public function getLogin() {
    return $this->postLogin();
}

public function postLogin() {
    $credentials = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );

    try {
        $user = Sentry::authenticate($credentials, false);

        if ($user) {
            //var_dump(array('flash' => 'Authentication failed'));
            //return Response::json(array('flash' => 'Authentication failed'), 401);
            return $user->toJson();
        }
    } catch (Exception $e) {
        return Response::json(array('flash' => 'Authentication failed'), 401);
    }
}

public function getLogout() {
    Sentry::logout();
    return Response::json(array('flash' => 'Logged out'), 200);
    //return Redirect::route('admin.login');
}
}

Have you tried something like this:

try {
    $user = Sentry::authenticate($credentials, false);

    if ($user) {
        return Response::json($user);
        # or
        Response::json($user)->send();
    }
} catch (Exception $e) {
    return Response::json(array('flash' => 'Authentication failed'), 401);
}

It turns out I was sending headers twice. I initially had trouble with CORS since I was connecting from an Angular JS front end running on local host to this laravel API also running on local host but on a different port. I was sending a header at the top of my Routes.php like this:

header('Access-Control-Allow-Origin', '*');

file even though my filters.php already had the code shown below:

App::before(function($request)
{
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {

    header('Access-Control-Allow-Origin', '*');
    header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials', 'true');

    exit;
}
});

App::after(function($request, $response)
{
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Allow', 'GET, POST, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Requested-With');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
return $response;
});

I hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM