Ich habe eine PHP-Datei, die einige Operationen mit den Daten und dann senden Sie die Daten in json
Format (angeblich).Json_encode liefert String anstelle von Objekt
Dann erhalte ich die Daten in einer js-Datei, mit Ajax. Es ist eine Cross-Domain-Operation, dann muss ich Jsonp verwenden.
Das Problem ist, dass ich den Fehler empfangen
Object {readyState: 4, status: 200, statusText: "success"} parsererror - Error: jQuery1123030211047915085665_1465277732410 was not called(…)
was ich glaube, das ist, weil ich nicht die Daten als JSON-Objekt, sondern als einfacher String empfangen (wenn ich Datentyp von JSONP ändern zum Text geht es an den .done
Block).
Was kann ich tun, um die Daten als JSON-Objekt zu empfangen, nicht als einfache Zeichenfolge?
Code:
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).')';
}
EDIT: Ich habe vergessen, den Javascript-Code zu schreiben, hier geht:
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=?',// 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 :
Gerade diese Zeile aktualisiert:
echo $_GET['callback'] . '('.json_encode($retorno,JSON_PRETTY_PRINT).')';
dieser
echo $_GET['callback'] . '('.json_encode($retorno,JSON_PRETTY_PRINT).');';
und der Fehler zeigt, ist nicht mehr. Es tritt jedoch nicht in die for
-Schleife ein und es werden keine Daten angezeigt. Ich habe console.log(data.dados.lenght)
verwendet und es gibt 'undefined' an mich zurück, so dass ich keine Schleife machen kann.
Irgendwelche Ideen?
Sie müssen auch JavaScript schreiben. – jeroen
Sorry, habe gerade die Frage aktualisiert. – Lioo