javascript - Json_encode returns string instead of object -


i got php file operations data , send data in json format(supposedly).

then receive data in js file, using ajax. it's cross domain operation, need use jsonp.

the trouble i'm receiving error

object {readystate: 4, status: 200, statustext: "success"} parsererror - error: jquery1123030211047915085665_1465277732410 not called(…)

which believe because i'm not receiving data json object simple string(when change datatype jsonp text goes .done block).

what can receive data json object, not simple string?

code:

php:

if ( $moeda ==='sek' ){   foreach($result $r){ //$result array result of sql query  //here verifications, depending on circunstance, calculate , replace  //the value of $r['price'] field.      if($r['currency'] === "sek"){         $valor = $r['tprice'];         $r['tprice'] = number_format($valor,2,'.','');      }else if ($r['currency'] === "brl"){         $dat = $r['emissao'];         $valor = $r['tprice'];           $r['tprice'] = number_format( ( convertebrl_sek($dat,$valor) ) ,2,'.','');       }else if ($r['currency'] === "usd"){         $dat = $r['emissao'];         $valor = $r['tprice'];         $r['tprice'] = number_format(( converteusd_sek($dat,$valor) ),2,'.','');           }else if ($r['currency'] === "eur"){         $dat = $r['emissao'];         $valor = $r['tprice'];             $r['tprice'] = number_format(( converteeur_sek($dat,$valor) ),2,'.','');          }     else{         echo 'error';     } $retorno['dados'] = $r;    // using callback because i'm using jsonp. echo $_get['callback'] . '('.json_encode($retorno,json_pretty_print).')';    } 

edit: forgot post javascript code, here goes:

code:

function buildtabledetail(p_sts,p_ar){       $('#tb_detail').empty();     return $.ajax({        type:'get',        crossdomain:true,        url:'http://localhost/files/montar_detail_local.php?callback=?',// use real url, localhost example        datatype:'jsonp',        data: {area:p_ar,               st:p_sts},        beforesend: function(){        $('#t_detail').css('opacity','1.0');              console.log(p_ar);         console.log(p_sts);        }           }).done(function(data){         //after data, build table, , format values here...          $('#tb_detail').empty();           console.log("area:"+p_ar);          (i = 0; < data.dados.length; i++) {               $('#tb_detail').append('<tr> <td>'+data.dados[i].proposal+                                     '</td><td class="h_detail old_no" >'+data.dados[i].old_no+                                     '</td><td class="h_detail emissao" style="white-space: nowrap;">'+data.dados[i].emissao+                                     '</td><td class="h_detail area">'+data.dados[i].area+                                     '</td><td class="h_detail country">'+data.dados[i].country+                                     '</td><td class="h_detail ec_name">'+data.dados[i].ec_name+                                     '</td><td class="h_detail distributo">'+data.dados[i].distributo+                                     '</td><td class="h_detail project">'+data.dados[i].project+                                     '</td><td>'+float2moeda(data.dados[i].tprice)+                                     '</td><td class="h_detail gm">'+data.dados[i].gm+                                     '</td><td >'+data.dados[i].prob+                                     '</td><td class="h_detail st">'+(data.dados[i].st).substr(0,1)+'</td></tr>');                   console.log(data.dados[i].proposal);                 console.log(data.dados[i].distributo);             } }) .fail(function(data, textstatus, errorthrown){         alert("erro na operação.");         console.log(data);         console.log(textstatus);         console.log(errorthrown);      })  } 

edit2:

just updated line:

echo $_get['callback'] . '('.json_encode($retorno,json_pretty_print).')';  

to this

echo $_get['callback'] . '('.json_encode($retorno,json_pretty_print).');';  

and error not showing anymore. however, it's not entering in for loop , it's not showing data. used console.log(data.dados.lenght) , returns 'undefined' me, can't loop.

any ideas?

try following :

  if ( $moeda ==='sek' ){   foreach($result $r){ //$result array result of sql query  //here verifications, depending on circunstance, calculate , replace  //the value of $r['price'] field.      if($r['currency'] === "sek"){         $valor = $r['tprice'];         $r['tprice'] = number_format($valor,2,'.','');      }else if ($r['currency'] === "brl"){         $dat = $r['emissao'];         $valor = $r['tprice'];           $r['tprice'] = number_format( ( convertebrl_sek($dat,$valor) ) ,2,'.','');       }else if ($r['currency'] === "usd"){         $dat = $r['emissao'];         $valor = $r['tprice'];         $r['tprice'] = number_format(( converteusd_sek($dat,$valor) ),2,'.','');           }else if ($r['currency'] === "eur"){         $dat = $r['emissao'];         $valor = $r['tprice'];             $r['tprice'] = number_format(( converteeur_sek($dat,$valor) ),2,'.','');          }     else{         echo 'error';     } $retorno['dados'][] = $r;   //append array  }    // using callback because i'm using jsonp. echo $_get['callback'] . '('.json_encode($retorno,json_pretty_print).');'; //echo valid call of callback