Php 在返回json时打印脚本
打印json编码文件中的数据时,它也会打印脚本。这个问题的解决办法是什么Php 在返回json时打印脚本,php,json,Php,Json,打印json编码文件中的数据时,它也会打印脚本。这个问题的解决办法是什么 <?php /* * Following code will get single product details * A product is identified by product id (pid) */ // array for JSON response $response = array(); $servername = "mysql11.000webhost.com"; $usernam
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
$servername = "mysql11.000webhost.com";
$username = "a1978721_test";
$password = "heavenhell1";
$dbhandle = mysql_connect($servername, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("a1978721_test",$dbhandle)
or die("Could not select examples");
// check for post data
if (isset($_GET["pid"])) {
$pid = $_GET['pid'];
// get a product from products table
$result = mysql_query("SELECT * FROM discount WHERE id = $pid");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$student = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$student["exam_roll"] = $row["exam_roll"];
$student["class_no"] = $row["class_no"];
$student["block_no"] = $row["block_no"];
$student["name"] = $row["name"];
// success
}
$response["success"] = 1;
// user node
$response["student"] = array();
array_push($response["student"], $student);
// echoing JSON response
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No student found";
// echo no users JSON
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No student found";
// echo no users JSON
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
}
header('Content-Type: application/json');
echo json_encode($response);
?>
它的输出是这样的,这正是我不想要的。我需要从输出中删除脚本部分。任何帮助都将不胜感激:)
{“成功”:1,“学生”:[{“考试卷”:“1212”,“班级号”:“121”,“单元号”:“1221”,“姓名”:“rohit”}]}
注释行并检查输出
//标题('Content-Type:application/json')
问题 您的主机提供商正在向您发送回的响应主体中注入广告。有两种方法可以解决此问题。第一个是从每个响应中删除它(当Javascript解析它时,只需删除前n行),或者您可以找到一种方法来解决系统的实现方式 从Javascript中删除它 我假设您正在接受Javascript中的响应(不过,同样的想法也适用于任何地方) 关闭分析功能
<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>
我不确定,但我猜你正在使用000台网络主机。如果您是,这里的任何方法都适用于您:
更改.htaccess中的附加规则
<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>
php_值自动_追加_文件无
它适用于此解决方案,但我正在搜索标题为('Content-Type:application/json')的解决方案;解释你为什么需要它。我猜您想将页面重定向到某个位置;)我只是好奇!我只是使用get参数来提取json数据。也许这里的解决方案可以帮助你
<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>