繁体   English   中英

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

[英]php header printing full html code on browser

我正在从 PHP 制作一个简单的 API。

代码片段:

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

每当执行此操作时,我都会在浏览器上收到此响应:

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

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

整个 HTML 代码打印在浏览器上。 当我删除header("Content-Type: application/json"); ,它得到修复并且 JSON 显示但在文本中。 我该如何解决?

当您删除内容类型 header 时,它并没有真正得到修复,但是 HTML 部分将变得“不可见”,因为内容类型将回退到默认的text/html 如果您检查页面的源代码,您会发现它仍然存在。

真正的解决办法是搜索上面的 HTML 的打印位置并删除。

暂无
暂无

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

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