简体   繁体   中英

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

Here is my JavaScript code:

$('#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:

[{"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"}]

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"}]

It is happening because white spaces . trim it before returning the response. 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 -

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.

Or something weired within the web server (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:

using var_dum($d) getting this response <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.

EDIT 2 :

you might try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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