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

时间:2018-07-17 15:25:06

标签: php angular laravel

我将Angular 2和Laravel PHP框架一起用于我的API。我的Angular 2项目在http://localhost:4200/上运行,而我的Laravel PHP Framework在http://localhost:80/laravel/public上运行

当我在Angular 2项目中运行它时:

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

我在浏览器中收到此错误:

  

无法加载http://localhost/laravel/public/api/v1/professionals/:   飞行前响应无效(重定向)

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

这是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();
    }

我在这里做错了什么?我的api调用有问题吗?

当我这样做时,请致电:

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

拨打电话以获取国家/地区有效!

当我转到URL:http://localhost/laravel/public/api/v1/professionals/

这是返回的内容:

enter image description here

请帮助!

以下是位于我的路线文件夹中的Laravel api.php文件中的路线:

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');
    });

});

1 个答案:

答案 0 :(得分:0)

问题是Django 2,您需要在服务器(后端)上启用CORS标头并允许使用适当的http方法。整个过程很可能在您的PHP层上或您用来提供数据的任何位置上。