0
I’m having a hard time finding the error, I’ve been looking here for hours. I am receiving a PHP JSON using Jquery, I can visualize it as can see below the result of the answer "date":
{
"sucesso": 1,
"dados": {
"BTC_YAMRUB": {
"result": 500000,
"url_redirect": "https:\/\/mysite.com\/?rid=1229109",
"https": 1,
"link_permanente": "mysite",
"nome": "MySite",
"exchange_rates": 500000,
"idade": "3 anos e 1 m\u00eas",
"wmid": null,
"pais_nome": "Brasil",
"moeda_from": "Bitcoin BTC",
"moeda_to": "Yandex money RUB",
"rank_from": "14",
"rank_to": "38",
"reviews": [{
"cliente_site": "52",
"positivo": "0",
"comentario": "0",
"negativo": "0"
}],
"offer": {
"from": 0,
"to": 0,
"in": 1,
"out": 500000,
"amount": 121436.1859,
"minfee": 0,
"fromfee": 0,
"tofee": 0,
"minamount": 0,
"param": "manual"
}
}
}
}
but while trying any of the lines below I get undefined
.
console.log(data[0].length);
console.log(data.length);
console.log(data.dados.length);
The javascript code:
function getRates()
{
$("#gif_load").show();
$.post(URL_SITE + '/api/exchangers', {from: $("#from").val(), to: $("#to").val()}, function(data, textStatus, xhr) {
if(textStatus == "success") {
$("#exchangers tbody tr").remove();
console.log(data.length); // undefined
console.log(Object.keys(data).length); // 2
if(data.dados.length > 0)
{
$.each(data.dados, function(index, el) {
console.log(el.result);
});
}
}
});
}
The code of PHP:
<?php
ini_set('display_errors', 1);
include_once(dirname(__FILE__)."/../includes/_conect.php");
$limit_blocktraill = 300;
header('Content-Type: application/json');
$url_invalid = URL_SITE . '/';
$url_valid = str_replace(":80", "", $url_invalid);
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && strpos($_SERVER["HTTP_REFERER"], $url_valid) !== false)
{
if(isset($_POST["from"]) && isset($_POST["to"]))
{
$from = mysqli_real_escape_string($_POST["from"]);
$to = mysqli_real_escape_string($_POST["to"]);
#--------------------MemCached-----------------------------------------
$memCached = new Memcached();
$memCached->addServer('localhost', 11211);
if ($dados = $memCached->get('rank_moeda')) {
if ($memCached->getResultCode() == Memcached::RES_NOTFOUND) {
$exchangers = 0;
}else{
$exchangers = $dados;
}
}
if(count($exchangers) > 0)
{
echo json_encode(array("sucesso" => 1, "dados" => $exchangers));
}else{
echo json_encode(array("sucesso1" => 1, "dados" => array()));
}
}else{
echo json_encode(array("sucesso2" => 0, "Erro" => "Error"));
}
}else{
echo json_encode(array("sucesso3" => 0, "Erro" => "Error"));
}
I checked the whole answer on https:/jsonlint.com/ and it’s OK. To some other test I can do to find out what’s going on?
It has how to put the JS code that makes and treats the request?
– Woss
For now, I edited the question with the JS code.
– PerduGames
I edited with PHP code too.
– PerduGames