简体   繁体   English

为什么我在ajax响应开始时得到??

[英]Why I am getting � at the start of ajax response?

Here is my JavaScript code: 这是我的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

        };
    }
},

My controller: 我的控制器:

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

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

The AJAX response I am getting: 我得到的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":"shoes"},{"tag_id":"19","tag_list":"scaper"}] [{ “TAG_ID”: “1”, “tag_list”: “#遵循”},{ “TAG_ID”: “2”, “tag_list”: “#all_shots”},{ “TAG_ID”: “3”, “tag_list”: “#instago”},{ “TAG_ID”: “4”, “tag_list”: “#风格”},{ “TAG_ID”: “5”, “tag_list”: “#TFLers”},{ “TAG_ID”: “6”, “tag_list”: “#遵循”},{ “TAG_ID”: “7”, “tag_list”: “#all_shots”},{ “TAG_ID”:” 8" , “tag_list”: “#instago”},{ “TAG_ID”: “9”, “tag_list”: “#风格”},{ “TAG_ID”: “10”, “tag_list”: “#TFLers”} ,{ “TAG_ID”: “11”, “tag_list”: “#igers”},{ “TAG_ID”: “12”, “tag_list”: “#女孩”},{ “TAG_ID”: “13”,“tag_list “:” #多彩 “},{” TAG_ID “:” 14" , “tag_list”: “#自然”},{ “TAG_ID”: “15”, “tag_list”: “#树”},{ “TAG_ID” : “16”, “tag_list”: “#绿色”},{ “TAG_ID”: “17”, “tag_list”: “#skylovers”},{ “TAG_ID”: “18”, “tag_list”: “鞋” },{ “TAG_ID”: “19”, “tag_list”: “scaper”}]

I want to remove these characters and get the response only which is sending from controller like below: 我想删除这些字符并获得仅从控制器发送的响应,如下所示:

[{"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"}] [{ “TAG_ID”: “1”, “tag_list”: “#遵循”},{ “TAG_ID”: “2”, “tag_list”: “#all_shots”},{ “TAG_ID”: “3”,“tag_list “:” #instago “},{” TAG_ID “:” 4" , “tag_list”: “#风格”},{ “TAG_ID”: “5”, “tag_list”: “#TFLers”},{ “TAG_ID” : “6”, “tag_list”: “#遵循”},{ “TAG_ID”: “7”, “tag_list”: “#all_shots”},{ “TAG_ID”: “8”, “tag_list”:“#instago “},{” TAG_ID “:” 9" , “tag_list”: “#风格”},{ “TAG_ID”: “10”, “tag_list”: “#TFLers”},{ “TAG_ID”: “11” 时, “tag_list”: “#igers”},{ “TAG_ID”: “12”, “tag_list”: “#女孩”},{ “TAG_ID”: “13”, “tag_list”: “#多彩”},{” TAG_ID “:” 14" , “tag_list”: “#性质”},{ “TAG_ID”: “15”, “tag_list”: “#树”},{ “TAG_ID”: “16”, “tag_list”:” #green “},{” TAG_ID “:” 17" , “tag_list”: “#skylovers”},{ “TAG_ID”: “18”, “tag_list”: “鞋”},{ “TAG_ID”: “19” “tag_list”: “scaper”}]

It is happening because white spaces . 它发生的原因是因为white spaces trim it before returning the response. 在返回响应之前trim它。 Try with - 尝试 -

echo trim($d);

Update 更新

This could also help but i am not sure about the reason the s on the output as some functions are generating the data and the data might coming from database, so somewhere those characters are prepended to the data - 这也可以帮助,但我不知道这是什么缘故上输出一些功能生成数据,并可以从数据库中来,这样的地方这些字符预先计划在数据的S -

echo trim($a, '�');

Try - 试试 -

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

Output - 输出 -

<pre class

There's nothing wrong with the code above. 上面的代码没有错。

Since these chars do appear, they are prepended after the echo $d - which might be caused by an output handler globally registered in PHP. 由于这些字符确实出现,它们在echo $d 之后被添加 - 这可能是由在PHP中全局注册的输出处理程序引起的。

Or something weired within the web server (nginx, apache, ...?). 或者在Web服务器中使用的东西(nginx,apache,......?)。

I would recommend to check that first before fiddling around in obviously proper three-liners. 我建议首先检查一下,然后摆弄明显适当的三线。

EDIT : 编辑

It seems, that everything what PHP is spitting out anyhow is getting surrounded by some HTML-Tags: 看起来,无论如何PHP正在吐出的一切都被一些HTML标签所包围:

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

This indicates, that there IS an output handler and any solution which tries to solve this by modifying $d will / must fail. 这表明, 一个输出处理程序和它试图通过修改$ d将会/必须失败来解决这个任何溶液。

EDIT 2 : 编辑2

you might try: 你可能会尝试:

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

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

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