When a user clicks on an icon, a certain emploee_id is transmitted to the server on the basis of which the server makes the calculations and if these calculations are correct, a pdf document should open in a new browser tab (which is currently happening).
But if validation fails, the user must be notified by a pop-up message describing the errors.

Here I can not figure out how to do it. The problem is that on the client responseType = 'blob' - if you change it, for example on json, the pdf document opens with errors.
Throughout the application, data is sent from the server in the form of json:
{"status_code": 200, "status": "success", "text": null, "data": [{...}, ...]}.
But the underlying code I can not fit under this format.

PS: I tried to convert the final output string to the server via base64_encode and pack everything into json of the form:

{ "status": "success", "binary_data": base64_encode($string) } 

and decode on the client through window.atob (string) and substitute it into the constructor new Blob (), but nothing comes out, the document opens with errors



Server code:

  // здСсь происходит нСсколько этапов Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ ΠΏΠΎ Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹ΠΌ ΠΏΡ€Π°Π²ΠΈΠ»Π°ΠΌ // Π² Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π΅ Ρ‡Π΅Π³ΠΎ Π² $errors формируСтся строка ΠΈΠ· ошибок ... if(!$this->form_validation->run('mto_rules')) { $errors .= validation_errors(); } if($errors !== '') { // здСсь Π½ΡƒΠΆΠ½ΠΎ ΠΊΠ°ΠΊ-Ρ‚ΠΎ ΡƒΠ²Π΅Π΄ΠΎΠΌΠΈΡ‚ΡŒ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ ΠΎΠ± ΠΎΡˆΠΈΠ±ΠΊΠ°Ρ… // Π²ΠΎ всСм ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΈ Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½ΠΎ: // $response['status_code'] = 200; // $response['status'] = 'error'; // $response['text'] = $errors; // .... // Ρ‚ΡƒΡ‚ ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ вычислСния ΠΊΠ°ΠΊΠΈΠ΅-Π»ΠΈΠ±ΠΎ // .... // ΠΈ Π² самом ΠΊΠΎΠ½Ρ†Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°, послС всСх ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΎΠΊ // $this->output->set_status_header($response['status_code']) // ->set_output($response); } // Π€ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ Π½Π΅ΠΊΠΈΠΉ html ΠΈ ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΠΌ Π² pdf Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΎΠΉ TCPDF ... ... $pdf->writeHTML($html, true, false, false, false, ''); // ΠΎΡ‚Π΄Π°Π΅ΠΌ Π±ΠΈΠ½Π°Ρ€Π½ΡƒΡŽ строку - Π’ΠΠ–ΠΠž!!! echo $pdf->Output('', 'S'); 

Client code:

  var xhr = new XMLHttpRequest() xhr.open('GET', 'my.php?emploee_id=' + emploee_id, true) xhr.responseType = 'blob' xhr.onload = function(e) { var blob = new Blob([this.response],{type: 'application/pdf'}) var url = window.URL.createObjectURL(blob) window.open(url) } xhr.send() 

    0