简体   繁体   English

php header 在浏览器上打印完整的 html 代码

[英]php header printing full html code on browser

I am making a simple API from PHP.我正在从 PHP 制作一个简单的 API。

Code Snippet:代码片段:

        elseif ($_GET["command"]="verifyconn"){
            header("Content-Type: application/json");
            $data=array("response" => "success");
            echo json_encode($data);
            exit;
        }

Whenever this is executed, I get this response on browser:每当执行此操作时,我都会在浏览器上收到此响应:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>{"response":"wrong_secret"}

The whole HTML code gets printed on browser.整个 HTML 代码打印在浏览器上。 When I remove header("Content-Type: application/json");当我删除header("Content-Type: application/json"); , it gets fixed and JSON displays but in text. ,它得到修复并且 JSON 显示但在文本中。 How can I fix it?我该如何解决?

It doesn't get really fixed when you remove the content type header, but the HTML part will become "invisible", because the content type will fall back to the default text/html ;当您删除内容类型 header 时,它并没有真正得到修复,但是 HTML 部分将变得“不可见”,因为内容类型将回退到默认的text/html if you check the source code of the page, you will see it's still there.如果您检查页面的源代码,您会发现它仍然存在。

The real solution is to search for where the above HTML is printed and remove it.真正的解决办法是搜索上面的 HTML 的打印位置并删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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