dongyunque2511 2018-07-17 15:25
浏览 72
已采纳

Angular 2无法加载预检的响应无效(重定向)[重复]

I am using Angular 2 with a Laravel PHP Framework for my API. My Angular 2 project is running on http://localhost:4200/ while my Laravel PHP Framework is running on http://localhost:80/laravel/public

When I run this in my Angular 2 Project:

getAllProfessionals():Observable<any> {
        return this.http.get(globals.baseUrl+'/professionals/', {headers:this.utilService.getHeadersJson()}).map(this.utilService.map);
    }

I get this error in my browser:

Failed to load http://localhost/laravel/public/api/v1/professionals/: Response for preflight is invalid (redirect)

globals.baseUrl is http://localhost/laravel/public/api/v1/

And here are the calls from this.utilService

getHeadersJson() {
        let headers = new Headers();
        headers.append('Accept', 'application/json');
        headers.append('Content-Type', 'application/json');
        headers.append('Authorization',  'Bearer ' + localStorage.getItem('token'));
        return headers;
    }   

    map(response: Response): any {
        return response.json();
    }

What am I doing wrong here? Is there something wrong with my api call?

When I do this get call:

getCountrys(filter):Observable<any> {
            return this.http.get(globals.baseUrl+'/country/' + filter, {headers:this.utilService.getHeadersJson()}).map(this.utilService.map);
        }

That call to get Countrys works!

When I goto the URL: http://localhost/laravel/public/api/v1/professionals/

This is what returns:

enter image description here

PLEASE HELP!

Here are the routes from Laravel api.php file located in my routes folder:

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::group([ 'prefix' => 'v1'], function () {

    Route::resource('register', 'v1\RegisterController');
    Route::resource('paintype', 'v1\PainTypeController');
    Route::resource('painlevel', 'v1\PainLevelController');
    Route::resource('painkiller', 'v1\PainKillersController');
    Route::resource('profession', 'v1\ProfessionsController');
    Route::resource('qualification', 'v1\QualificationsController');
    Route::resource('specialities', 'v1\SpecialitiesController');
    Route::resource('problems', 'v1\ProblemsController');
    Route::resource('user', 'v1\UserController');
    Route::post('login', 'v1\AuthController@login');
    Route::post('professionals', 'v1\ProfessionalsController@store');
    Route::post('patients', 'v1\PatientsController@store');
    Route::get('country/{text}', 'v1\LocationController@getCountries');
    Route::get('region/{id}', 'v1\LocationController@getRegions');
    Route::get('city/{id}', 'v1\LocationController@getCities');
    Route::get('reviews/{username}', 'v1\ReviewController@show');

    Route::group(['middleware' => ['before' => 'jwt.auth']], function () {
        Route::resource('post-job', 'v1\PostJobController');
        Route::post('reviews', 'v1\ReviewController@store');
        Route::get('profile', 'v1\AuthController@profile');
        Route::get('jobs/listjob', 'v1\PostJobController@getMyJobs');
    });

});
</div>
  • 写回答

1条回答 默认 最新

  • douke1905 2018-07-17 15:26
    关注

    The issue is CORS you need to on the server(backend) enable the CORS headers and allow the appropriate http methods. The entire thing is most likely on your PHP layer or whatever you use to serve the data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?