2016-06-07 110 views
-4

我有一个PHP文件,对数据做一些操作,然后以json格式发送数据(据推测)。Json_encode返回字符串而不是对象

然后我使用ajax接收js文件中的数据。这是一个跨域操作,那么我需要使用jsonp。

麻烦的是,我收到了错误

Object {readyState: 4, status: 200, statusText: "success"} parsererror - Error: jQuery1123030211047915085665_1465277732410 was not called(…)

,我相信这是因为我没有收到数据作为JSON对象,但作为一个简单的字符串(当我从JSONP更改数据类型将其发送到.done块)。

我能做些什么来接收数据作为json对象,而不是一个简单的字符串?

代码:

PHP:

if ($moeda ==='SEK'){ 

foreach($result as $r){ //$result is an array with the result of a sql query 

//here I do some verifications, that depending on the circunstance, calculate and replace 
//the value of the $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 the GET callback because I'm using jsonp. 
echo $_GET['callback'] . '('.json_encode($retorno,JSON_PRETTY_PRINT).')'; 


} 

编辑: 我忘了张贴的JavaScript代码,这里有云:

代码:

function buildTableDetail(p_sts,p_ar){ 
    $('#tb_detail').empty(); 
    return $.ajax({ 
     type:'GET', 
     crossDomain:true, 
     url:'http://localhost/files/montar_detail_local.php?callback=?',// I use a real url, localhost just for the 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 I get the data, I build a table, and format some values here... 
     $('#tb_detail').empty(); 

     console.log("AREA:"+p_ar); 
     for (i = 0; i < 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 :

刚刚更新了此行:

echo $_GET['callback'] . '('.json_encode($retorno,JSON_PRETTY_PRINT).')'; 

这个

echo $_GET['callback'] . '('.json_encode($retorno,JSON_PRETTY_PRINT).');'; 

和错误没有显示了。但是,它没有输入for循环,它没有显示任何数据。我使用了console.log(data.dados.lenght),它向我返回'undefined',所以我无法循环。

任何想法?

+0

您需要发布的JavaScript以及。 – jeroen

+0

对不起,刚更新了这个问题。 – Lioo

回答

1

尝试以下操作:

if ($moeda ==='SEK'){ 

foreach($result as $r){ //$result is an array with the result of a sql query 

//here I do some verifications, that depending on the circunstance, calculate and replace 
//the value of the $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 to the array 

} 
    // using the GET callback because I'm using jsonp. 
echo $_GET['callback'] . '('.json_encode($retorno,JSON_PRETTY_PRINT).');'; //echo the valid call of the callback 
+0

将在这里尝试并在一分钟内回复 – Lioo

+0

嘿,谢谢!它解决了一半的问题! **我不再收到错误**但它没有返回给我。 – Lioo

+0

当你控制数据时你没有收到什么信息? – madalinivascu

2

JSON不过是一个字符串。如果您使用jQuery将json字符串转换为对象,请在JavaScript中使用$ .parseJSON。

+0

是的,我知道,但我必须使用json_encode在服务器和客户端之间执行其他数据操作,并且它通常会作为Json对象接收。包括这个例子。在我写完if和做转换之后,它开始大声呼喊这个错误。在此之前,我只是做一个SQL查询,获取到一个数组,然后使用json_encode作为'json'对象发送数据,它工作 – Lioo

0

尝试使用JSON_FORCE_OBJECT代替JSON_PRETTY_PRINT

这样的事情。

echo $_GET['callback'] . '('.json_encode($retorno,JSON_FORCE_OBJECT).')';

+0

嗨,我已经尝试过,但它不起作用。它一直在喊同样的错误 – Lioo

+0

你尝试使用'dataType:'json','而不是'dataType:'jsonp',' – Archish

+0

是的,它给我一个跨域错误。这是一个跨域操作,那么我必须使用jsonp。在这种情况下json不起作用 – Lioo

相关问题