1

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.sstatic.net/mF8pN.png https://i.sstatic.net/CYLt6.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:

Chrome error message

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

    }
10
  • the problem is with the params you send. Can you show how params look like when sent from Postman? Commented Aug 11, 2019 at 16:47
  • Even the error message says things about array_merge() issue if you notice. Commented Aug 11, 2019 at 16:47
  • There is nothing to do with js. Looks like there is a problem with email rendering. Please add more Laravel code to your question (controller action, email sending). Commented Aug 11, 2019 at 17:45
  • @vivek_23 It look like this i.imgur.com/ZgLJA20.png . I also think that I has to do with the params I send, because when I delete the params from Postman, I get the same error when I test the function on Ionic app. Commented Aug 11, 2019 at 18:55
  • @AndriyLozynskiy I've updated the question with the PlayerController php code that render and send the email. Commented Aug 11, 2019 at 18:56

1 Answer 1

1

The second parameter for PDF::loadView() must be an array.

Try to change this

$pdf = PDF::loadView('templatePdf', $player);

into

$pdf = PDF::loadView('templatePdf', ['player'=>$player]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked. I also needed to change all the other params from Player like this $player['id']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.