I'm trying to make a http post request to my Laravel API from Ionic 4. I've made other Post successfully before with the same API. But for some reason I can't make this one work.
The method on the Laravel backend receives an ID and then sends a email with a PDF attached.
I've test it with Postman tool and it works fine. But when I test on the Ionic 4 App it doesn't work.
I tried changing the headers with different options without luck.
I have set the CORS on Laravel backend like this:
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
die();
}
On Postman it works as expected: https://i.imgur.com/sNuJCzK.png https://i.imgur.com/ZgLJA20.png
This is my Player.service.ts:
pdf(idPlayer): Observable<any> {
let json = {
idplayer : idPlayer
};
let params = "json=" + JSON.stringify(json);
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Authorization' : localStorage.getItem("token"),
'Content-Type' : 'application/x-www-form-urlencoded',
})
};
console.log("PDF PARAMS:");
console.log(params);
return this._http.post('http://127.0.0.1:8000/api/pdf/email', params, httpOptions);
}
This is the function that calls the service on my Tab-Profile.page.ts:
enviarPdf() {
this._playerService.pdf(this.player._id).subscribe(
response => {
console.log("SEND PDF:");
console.log(response);
},
error => {
console.log("error sendPDF:");
console.log(error);
});
}
This is the error on Chrome console:
EDIT:
Here is my php function in PlayerController.php:
public function ToPdf(Request $request) {
// Obtener usuario identificado
$token = $request->header('Authorization');
$jwtAuth = new \JwtAuth();
$checkToken = $jwtAuth->checkToken($token);
$user = $jwtAuth->checkToken($token, true);
$json = $request->input('json', null);
$params_array = json_decode($json, true);
$id_player = $params_array['idplayer'];
$player = Player::find($id_player);
$pdf = PDF::loadView('templatePdf', $player);
$pdf_name = $player->id . '.pdf';
$content = $pdf->output();
\Storage::disk('players')->put('/pdf/' . $pdf_name, $content);
$exists = \Storage::disk('player')->exists('/pdf/' . $pdf_name);
if (!$exists) {
$data = array(
'code' => 400,
'status' => 'error',
'message' => 'Error PDF.',
'pdf' => $pdf_name
);
}
else {
$email_params = array(
'userName' => $user->name,
'playerId' => $player->id,
'playerName' => $player->name,
'playerSurname' => $player->surname,
);
// Mail::to($user->email)->queue(new EmailPdf($email_params));
Mail::to($user->email)->queue(new EmailPdf($email_params));
\Storage::disk('players')->delete('/pdf/' . $pdf_name);
$data = array(
'code' => 200,
'status' => 'success',
'message' => 'Email sended successfully.',
);
}
return response()->json($data, $data['code']);
}