简体   繁体   中英

Make PHP output plain text

So I recently installed an apache2 web server with php5, and when trying to output plain JSON, a page is generated already containing <html> , <head> and <body> tags, like so:

<html>
  <head></head>
  <body>
    { "text": "some text" }
  </body>
</html>

instead of giving me this:

{ "text": "some text" }

with my php code being:

<?php printf('{ "text": "some text" }'); ?>

I have tried looking online to find how to disable this with no result.
Any help is welcome.

If you don't output some html code, there will be no html code. PHP or Apache don't render html on their own. Probably your browser add the html Tags, if you view your source in the dev tools.

But if you output some json data, you should also set the correct mime-type header like so:

header('Content-Type: application/json');

Doing this, your browser (or other clients) know, you send them some json strings.

Where did you get that html code? I mean: Is that html what you get when you ctrl+U to check your code? Because maybe your browser is actually receiving the bare json code, and it's adding the html by himself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM