1

I am building a Laravel 5 REST API. And the angular application conduct JSONP CORS request to the api, but I get Error 302 and redirect to '/'. The server has correct CORS configuration, I don't know where is wrong.

The general information of the request:

Request URL:http://example.com/api/getinfo?_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9&callback=angular.callbacks._0
Request Method:GET
Status Code:302 Found

The request headers:

Access-Control-Allow-Headers:Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,Content-Length, Accept, x-csrf-token, origin
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Length:380
Content-Type:text/html; charset=UTF-8
Date:Fri, 15 Apr 2016 18:21:42 GMT
Location:http://example.com
Server:nginx
Set-Cookie:laravel_session=4aba180c69376045bad0e16c185ac60568378dcb; expires=Fri, 15-Apr-2016 20:21:42 GMT; path=/; httponly

1 Answers1

0

In Laravel, Error 302 (Redirect to certain url) often goes with user status validation or other middlewares. For example, in the following code, if a user does not have admin role but make a request through isAdmin middleware, he will be redirected to the index page.

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\RedirectResponse;

class IsAdmin {

public function handle($request, Closure $next)
{

    if (session('statut') === 'admin')
    {
        return $next($request);
    }
    return new RedirectResponse(url('/'));
}
}

Thus, I suggest that you should check if your REST API has some middlewares in the route or controllers.

Hongbin Wang
  • 1,159
  • 2
  • 14
  • 34