【问题标题】:Why I am getting � at the start of ajax response?为什么我在 ajax 响应开始时得到�?
【发布时间】:2015-07-09 20:29:15
【问题描述】:

这是我的 JavaScript 代码:

$('#tags').select2({
tags: true,
tokenSeparators: [','],
createSearchChoice: function (term) {
    return {
        id: $.trim(term),
        text: $.trim(term) + ' (new tag)'
    };
},
ajax: {
    url: '<?php echo site_url('home_page/get_tags');?>',
    dataType: 'json',
    data: function(term, page) {
        return {
            q: term
        };
    },
    results: function(data, page) {
        alert(data);
        return {

            results: data

        };
    }
},

我的控制器:

public function get_tags()
{            
    $data=  $this->common_model->get_tags_list();

    $d = json_encode($data);
    echo $d;
}

我得到的 AJAX 响应:

����������[{"tag_id":"1","tag_list":"#follow"},{"tag_id":"2","tag_list":"#all_shots" },{"tag_id":"3","tag_list":"#instago"},{"tag_id":"4","tag_list":"#style"},{"tag_id":"5"," tag_list":"#TFLers"},{"tag_id":"6","tag_list":"#follow"},{"tag_id":"7","tag_list":"#all_shots"},{"tag_id ":"8","tag_list":"#instago"},{"tag_id":"9","tag_list":"#style"},{"tag_id":"10","tag_list":"# TFLers"},{"tag_id":"11","tag_list":"#igers"},{"tag_id":"12","tag_list":"#girl"},{"tag_id":"13" ,"tag_list":"#colorful"},{"tag_id":"14","tag_list":"#nature"},{"tag_id":"15","tag_list":"#tree"},{ "tag_id":"16","tag_list":"#green"},{"tag_id":"17","tag_list":"#skylovers"},{"tag_id":"18","tag_list": "鞋子"},{"tag_id":"19","tag_list":"scaper"}]

我想删除这些字符并仅获取从控制器发送的响应,如下所示:

[{"tag_id":"1","tag_list":"#follow"},{"tag_id":"2","tag_list":"#all_shots"},{"tag_id":"3" ,"tag_list":"#instago"},{"tag_id":"4","tag_list":"#style"},{"tag_id":"5","tag_list":"#TFLers"},{ "tag_id":"6","tag_list":"#follow"},{"tag_id":"7","tag_list":"#all_shots"},{"tag_id":"8","tag_list": "#instago"},{"tag_id":"9","tag_list":"#style"},{"tag_id":"10","tag_list":"#TFLers"},{"tag_id":" 11","tag_list":"#igers"},{"tag_id":"12","tag_list":"#girl"},{"tag_id":"13","tag_list":"#colorful"} ,{"tag_id":"14","tag_list":"#nature"},{"tag_id":"15","tag_list":"#tree"},{"tag_id":"16","tag_list ":"#green"},{"tag_id":"17","tag_list":"#skylovers"},{"tag_id":"18","tag_list":"shoes"},{"tag_id": "19","tag_list":"scaper"}]

【问题讨论】:

  • 是否注册了任何 PHP 输出处理程序?
  • 猜测是UTF8字节序标记,保存不带字节序标记的文件stackoverflow.com/questions/3255993/…
  • 当你die($d);时你会得到什么
  • @NullPoiиteя 什么都没有,因为 $id 没有定义。
  • 对不起,你死后会得到什么($d)?

标签: javascript php ajax json codeigniter


【解决方案1】:

这是因为white spacestrim 在返回响应之前。试试 -

echo trim($d);

更新

这也可能会有所帮助,但我不确定输出中 s 的原因,因为某些函数正在生成数据并且数据可能来自数据库,因此在某处这些字符被添加到数据中 -

echo trim($a, '�');

试试 -

$a = "����������<pre class";
echo trim($a, '�');

输出 -

<pre class

【讨论】:

  • @Asif_SE 你能在php端回显字符串(不是在得到响应之后)并显示输出吗?
  • $d 是json_encode php 函数的结果。这根本不会生成这些字符,因此“修剪”不会解决问题。此外,它不是空格,而是一些 8 位字符。
  • @AxelAmthor 这些字符被添加到$d
  • 所以任何建议请我删除这些字符
  • $d = json_encode($data); ob_clean(); ob_end_clean(); // 调用次数取决于堆叠输出处理程序的数量,echo $d;出口();这对我有用
【解决方案2】:

上面的代码没有问题。

由于确实出现了这些字符,它们被添加在echo $d 之后 - 这可能是由在 PHP 中全局注册的输出处理程序引起的。

或者 Web 服务器中的一些奇怪的东西(nginx、apache、...?)。

我建议先检查一下,然后再摆弄显然合适的三线。

编辑

看来,无论如何 PHP 吐出的所有东西都被一些 HTML-Tags 包围了:

使用 var_dum($d) 获得此响应 ����������<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'[{&quot ....

这表明存在一个输出处理程序,任何试图通过修改 $d 来解决此问题的解决方案都将/必须失败。

编辑 2

你可以试试:

 ....
 ob_end_clean();
 ob_end_clean();  // number of calls depends on number of stacked output handlers,
 echo $d;
 exit(0);

【讨论】:

  • 我正在与 codeigniter 合作修复输出处理程序有什么建议吗?
  • 很遗憾没有使用 ci,但这个 print_r(ob_list_handlers()); 可能是一个起点
  • 你好阿克塞尔阿姆索尔。 $d = json_encode($data); ob_clean(); ob_end_clean(); // 调用次数取决于堆叠输出处理程序的数量,echo $d;出口();这对我有用,非常感谢,伙计
猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 2020-12-12
  • 2016-11-22
  • 2019-06-28
  • 2013-01-24
  • 2017-03-12
相关资源
最近更新 更多