I have an api with laravel 5.7 and I am making a request of type GET with ionic 4 to laravel. this request works very well.
I want to receive the token in each request to the api. in a POST requests this works but in the GET requests when trying to pass the headers or options returns an error:
Access to XMLHttpRequest at ‘http://xxxxxx.com’ from origin ‘http: // localhost: 8100’ has been blocked by CORS policy: Response to preflight request does not pass access control check: No 'Access-Control-Allow -Origin 'header is present on the requested resource. core.js: 1449 ERROR Response {_body: ProgressEvent, status: 0, ok: false, statusText: “”, headers: Headers, …}
ionic 4
import { Injectable } from ‘@angular/core’;
import { Http, Response, Headers, RequestOptions } from ‘@angular/http’;
let headers = new Headers (
{
‘Content-Type’: ‘application / json’,
‘Authorization’: ‘H3fiTxeuyUL2jOQa3lMIhzZQeDgGmITQ9dnjc8dZzl4r5’
});
this.options = new RequestOptions ({headers: headers});
let url = this.url + ‘mylink’;
return this.http.get (url, {options}). map ((res: Response) => {
let data = res.json ();
return data;
});
i have laravel 5.7
and i have cors.php like this:
namespace App \ Http \ Middleware;
use Closure;
class Cors
{
/ **
* Handle an incoming request.
*
* @param \ Illuminate \ Http \ Request request
* @param \ Closure next
* @return mixed
* /
public function handle ( request, Closure next)
{
return next ( request) -> header (‘Access-Control-Allow-Origin’, ‘*’)
-> header (‘Access-Control-Allow-Headers’, ‘X-PINGOTHER, Content-Type, Authorization, Content-Length, X-Requested-With’)
-> header (‘Access-Control-Allow-Methods’, ‘GET, POST, PUT, PATCH, DELETE, OPTIONS’);
}
}