0

On localhost xamp, it prints fine. But same script, when i uploaded it on server (cpanel hostgator php7) it stays without formatting.

Here is my code, data are same and fine. Chrome and not "View page source". Php7.3 on hosting, php 8 on localhost, no errors

header('Content-Type: application/json; charset=utf-8');
echo(json_encode($data,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) );

Heres screenshot on hosting and on localhost

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
erik parker
  • 1
  • 2
  • 3
  • 1
    To me that makes no sense. How are you looking at the output? In the browser? In the source code of the browser? Using the developer tools? And which browser are you using? (different browsers handle json content differently) – KIKO Software Mar 27 '22 at 09:19
  • Same browser - chrome, localhost formated it, on server doesnt – erik parker Mar 27 '22 at 09:25
  • Sorry, my mistake. I asked multiple questions, so it makes sense you only answered the last one. Could you now address this one: "How are you looking at the output?" The options I could think of were: "In the browser? In the source code of the browser? Using the developer tools?". – KIKO Software Mar 27 '22 at 09:29
  • 1
    To me it does not look like your server is sending the content-type header correctly. You can confirm that this is the case yourself when the browser uses the monospaced font locally (which is to indicate that chrome determined that it is showing some sort of code) and it is using the default web font on the server. You can also confirm this by looking at the response header. Now the why (given the information provided here) can be anyone's guess – apokryfos Mar 27 '22 at 09:38
  • This might be a [headers already send](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987) error. – KIKO Software Mar 27 '22 at 09:46
  • No errors at all. No blank, white spaces – erik parker Mar 27 '22 at 10:18
  • Do not add solved to your question. Solutions go in the answer space below – Dharman Mar 27 '22 at 11:33

1 Answers1

0

I assume the code in your question is incomplete because there's a lot more code that you left out. I would suggest to make a Minimal, Reproducible Example, to check your findings. Something like this:

<?php

$data = ['row1' => 'content1',
         'row2' => 'content2',
         'row3' => 'content3'];

header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

Put that in a test.php file and run it both locally and on the server.

In Chrome use "View page source" to check the result.

My guess is that there's no difference in the output. This would mean that the problem is somewhere else in your code.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33