1
I have a question, how to rescue the values, which are transformed into json, into javascript? I think it’s a more elegant way to communicate both. I’ve been seeing a lot of gossip on the internet.
$json_data = array(
"dados" => $Partner->listaParceiro($id) ,
);
echo json_encode($json_data);
Basically, I just send a json with php. How can I get this data with javascript/ajax?
$.ajax({
type: 'GET',
url: "http://127.0.0.1/ecoLicenseLayout/send-data.php",
dataType: 'json',
contentType: 'application/json',
crossDomain: true,
cache:false,
success: function(data)
{
},
error:function(jqXHR, textStatus, errorThrown){
alert('Erro ao carregar');
console.log(errorThrown);
}
});
Above basically the ajax I’ve been trying
Generated error: Syntaxerror: Unexpected token < in JSON at position 0(...)
Content of my page
I need to take the select from the "Third" list and send it to another page. I thought of generating a json and recovering this json with javascript
<?php
error_reporting(E_ALL);
require_once("classes/Partner.php");
require_once("classes/Clicks.php");
if(isset($_GET['id']) && $_GET['id']) {
$id = $_GET['id'];
$Partner = new Partner();
$Cliques = new clicks();
date_default_timezone_set('America/Sao_Paulo');
$dataRegistro = date("Y-m-d H:i:s");
$Cliques->setDataRegistro($dataRegistro);
$Cliques->setIdPartner($id);
$Partner->updateContCliques($id);
//$Partner->listaParceiro($id);
$json_data = array(
"dados" => $Partner->listaParceiro($id) , // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
);
echo json_encode($json_data);
$Cliques->inserir();
}
else{
header("Location: reg-partner.php");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
require_once("cabecalho.php");
?>
</head>
</html>
<body>
<!-- /. WRAPPER -->
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- METISMENU SCRIPTS -->
<script src="assets/js/jquery.metisMenu.js"></script>
<!-- CUSTOM SCRIPTS -->
<script src="assets/js/custom.js"></script>
<script type="text/javascript">
$.getJSON('http://127.0.0.1/ecoLicenseLayout/send-data.php?id=2', null, function (data) {
alert(data.results); // Resultado: "Av. 7 de Setembro, S/N"
})
/*
var myUrl = encodeURIComponent("http://ecoprintq.com/index.php/partnerApplication/create");
var dados = "User_full_name:aaaaaaa&User_institution:sssss"
$.ajax({
url: "webproxy.php?url=" + myUrl,
data: dados,
crossDomain:true,
type: "GET",
timeout: 30000,
dataType: "text", // "xml", "json"
success: function(data) {
window.location.href = "webproxy.php?url=" + myUrl + "&" + dados;
},
error: function(jqXHR, textStatus, ex) {
alert(textStatus + "," + ex + "," + jqXHR.responseText);
}
});*/
var conteudoJSON;
$.ajax({
type: 'GET',
url: "http://127.0.0.1/ecoLicenseLayout/send-data.php",
dataType: 'json',
contentType: 'application/json',
crossDomain: true,
cache:false,
success: function(data)
{
conteudoJSON = data;
console.log(coneudoJSON);
},
error:function(jqXHR, textStatus, errorThrown){
alert('Erro ao carregar');
console.log(errorThrown);
}
});
</script>
</body>
================= Editing the post ============================
With what you said, it is the json that came... it is not the json that I expected
================ 2 EDITION =========================
My current code
<?php
error_reporting(E_ALL);
require_once("classes/Partner.php");
require_once("classes/Clicks.php");
if(isset($_GET['id']) && $_GET['id']) {
$id = $_GET['id'];
$Partner = new Partner();
$Cliques = new clicks();
date_default_timezone_set('America/Sao_Paulo');
$dataRegistro = date("Y-m-d H:i:s");
$Cliques->setDataRegistro($dataRegistro);
$Cliques->setIdPartner($id);
$Partner->updateContCliques($id);
//$Partner->listaParceiro($id);
$json_data = array(
"dados" => $Partner->listaParceiro($id) // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
);
echo json_encode($json_data);
$Cliques->inserir();
}
else{
header("Location: reg-partner.php");
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
require_once("cabecalho.php");
?>
</head>
<body>
<!-- /. WRAPPER -->
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- METISMENU SCRIPTS -->
<script src="assets/js/jquery.metisMenu.js"></script>
<!-- CUSTOM SCRIPTS -->
<script src="assets/js/custom.js"></script>
<script type="text/javascript">
var myUrl = window.location;
.ajax({
type: 'GET',
url: myUrl
data: {id:confirm("Insira o id desejado:")},
//dataType: 'json',
//contentType: 'application/json',
crossDomain: true,
cache: false,
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Erro ao carregar');
console.log(errorThrown);
}
});
</script>
</body>
<?php
}
?>
</html>
If in the success of the function put,
console.log(data);
something appears on the console?– Miguel
Sorry, I forgot to measure the error, I edited in the post
– Gabriel falieri
And nothing shows up
– Gabriel falieri
And you’re sure there’s something being returned from the server (
echo json_encode($json_data);
) does what’s supposed to?– Miguel
Yes, of course
– Gabriel falieri