返回json时打印脚本

时间:2015-07-10 05:12:16

标签: 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";
$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);
?>

它的输出就像这样,这正是我不想要的。我需要从输出中删除脚本部分。任何帮助将不胜感激:)

{"success":1,"student":[{"exam_roll":"1212","class_no":"121","block_no":"1221","name":"rohit"}]}    
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

2 个答案:

答案 0 :(得分:1)

问题

您的托管服务提供商会将广告注入您要发回的回复正文中。有两种方法可以解决这个问题。第一个是你从每个响应中删除它(只需在你的Javascript解析时删除前n行),或者你可以找到一种方法来解决系统的实现方式。

从Javascript中删除

我假设你在Javascript中提取响应(但是同样的想法会适用于任何地方)。

// break the textblock into an array of lines
var lines = jsonResponse.split('\n');
// remove one line, starting at the first position
// the removing the next 4 elements.
lines.splice(0,4);
// join the array back into a single string
var jsonResponse = lines.join('\n');

关闭Google Analytics

我不确定,但我猜你正在使用000webhosts。如果您愿意,这里的任何方法都适合您:

Webhoster inserts a javascript which brokes my code how to remove it?

更改.htaccess中的附加规则

<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>

答案 1 :(得分:0)

注释该行并检查输出

  

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