4

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

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

3

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
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • @Asif_SE can you echo the string on php side (not after getting the response) and display the output? – Sougata Bose Apr 30 '15 at 08:31
  • $d is the result of `json_encode` php function. This is not generating these chars at all, thus a "trim" will not solve the problem. Furthermore, it's not whitespaces but some 8-bit chars. – Axel Amthor Apr 30 '15 at 08:34
  • @AxelAmthor those characters are prepended to `$d`. – Sougata Bose Apr 30 '15 at 08:37
  • so any suggestion plz I am tied to remove these characters – Asif Gujjar Apr 30 '15 at 08:37
  • @sgtBOSE ... and what are you telling me? The OPs question is how to get rid of them, implying that someone knows where they are coming from. "trimming" would only cure symptoms and doesn't help at all in this case either. – Axel Amthor Apr 30 '15 at 08:39
  • using var_dum($d) getting this response ����������
    string '[{&quot
    ;tag_id":"1","tag_list":"#follow"},{"tag_id":"2&quot
    ;,"tag_list":"#all_shots"},{"tag_id":"3","tag_list&quot
    ;:"#instago"},{"tag_id":"4","tag_list":"#style"},{
    "tag_id":"5","tag_list":"#TFLers"}
    – Asif Gujjar Apr 30 '15 at 08:40
  • var_dump($data) showing this response ����������
    array (size=19)
      0 => 
        array (size=2)
          'tag_id' => string '1' (length=1)
          'tag_list' => string '#follow'
     (length=7)
    – Asif Gujjar Apr 30 '15 at 08:46
  • Then you should add the code of `get_tags_list()` also. – Sougata Bose Apr 30 '15 at 08:47
  • public function get_tags_list() { $q=$this->db->query("Select * from tags"); return $q->result_array(); – Asif Gujjar Apr 30 '15 at 08:51
  • please try this: `$test ="TESTSTRING"; var_dump($test);`and post the output please. – Axel Amthor Apr 30 '15 at 08:51
  • @sgtBOSE this is nonsense, sorry. – Axel Amthor Apr 30 '15 at 08:57
  • @AxelAmthor so what sense do you have to solve this? Please do it then. – Sougata Bose Apr 30 '15 at 08:58
  • $test ="TESTSTRING"; var_dump($test); response of this ....����������
    string 'TESTSTRING'
     (length=10)
    
    – Asif Gujjar Apr 30 '15 at 09:00
  • 1
    $d = json_encode($data); ob_clean(); ob_end_clean(); // number of calls depends on number of stacked output handlers, echo $d; exit(); it's work for me – Asif Gujjar Apr 30 '15 at 10:42
2

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);
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • I am working with codeigniter any suggestion to fix output handler ? – Asif Gujjar Apr 30 '15 at 09:14
  • unfortunatelly not with ci, but this `print_r(ob_list_handlers());` might be a starting point – Axel Amthor Apr 30 '15 at 09:24
  • 1
    Hello Axel Amthor . $d = json_encode($data); ob_clean(); ob_end_clean(); // number of calls depends on number of stacked output handlers, echo $d; exit(); it's work for me many thanks man – Asif Gujjar Apr 30 '15 at 10:41