【问题标题】:Print script along while returning json在返回 json 的同时打印脚本
【发布时间】:2015-07-10 05:12:15
【问题描述】:

从文件中打印 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 -->

【问题讨论】:

    标签: php json


    【解决方案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');
    

    关闭分析

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

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

    更改 .htaccess 中的附加规则

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

    【讨论】:

      【解决方案2】:

      注释该行并检查输出

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

      【讨论】:

      • 它适用于这个解决方案,但我正在寻找带有 header('Content-Type: application/json'); 的解决方案
      • 解释你为什么需要它。我猜你想将页面重定向到某个地方;)
      • 我只是好奇!我只是使用 get 参数来提取 json 数据。
      • stackoverflow.com/questions/31084576/… 可能是这里的解决方案可以帮助你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 2015-10-18
      相关资源
      最近更新 更多