Why does the XHR succeed and show in chromeDevTools but is undefined and cant be parsed by JSON.parse()
up vote
1
down vote
favorite
here's the script:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
})
}).then((data) => {
data = JSON.parse(data)
console.log("data for table is", data)
$("#requestTable").html(data);
})
}
What is returned from "getRequests.php" is markup in JSON.
Just for Reference, I'll post the backend code so you can look up what is put into the response. "$result" is what is returned at the end of the php.
<?php
/*
================================================================================
Wennn diese Datei aufgerufen wird und der Wert "ID" als Int übergeben wurde wird
eine Tabelle, die alle Anfragen anzeigt, zurückgegeben.
================================================================================
*/
if(isset($_POST["ID"])){
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
session_start();
$userRole = $connection->query("
SELECT name
FROM rolle
WHERE id = (
SELECT rolle
FROM benutzer_rollen
WHERE benutzer='".$_POST["ID"]."'
)
")->fetchAll()[0][0];
if($userRole == "admin"){
$result .= "<thead><tr><td>Von</td><td>Bis</td><td>Benutzer</td><td>Raum/Platz
</td><td>Aktionen</td></tr></thead><tbody>";
$allReservation = $connection->query("
SELECT id, anfang, ende, benutzer, arbeitsplatz
FROM reservierung
WHERE status='angefragt'
")->fetchAll();
foreach($allReservation as $row){
$user = $connection->query("
SELECT name, vorname
FROM benutzer
WHERE id='".$row["benutzer"]."'
")->fetchAll()[0];
$position = $connection->query("
SELECT raum, nummer
FROM arbeitsplatz
WHERE ID = '".$row["arbeitsplatz"]."'
")->fetchAll()[0];
$raumbild = $connection->query("
SELECT bild
FROM raum
WHERE name ='".$position["raum"]."'
")->fetchAll()[0][0];
$result .= "<tr><td>".date("d.m.y",strtotime($row["anfang"]))."</td><td>"
.date("d.m.y",strtotime($row["ende"]))."</td><td>".$user["name"]." "
.$user["vorname"]."</td><td><a>".$position["raum"]."/"
.$position["nummer"]."<div><img class="hoverImage"src="".$raumbild.
"" /></div></a></td><td><div class="form-inline form-horizontal">
<select id="statusDrop".$row["id"]."" class="form-control">
<option>genehmigen</option><option>ablehnen</option></select>
<button class='btn btn-default' onclick="submitStatus(".$row["id"].
");">Ok</button></div></td></tr>";
}
$result .= "</tbody></table>";
echo json_encode($result);
}
}
?>
In the chromeDevTools, the response is displayed correctly as HTML elements ("" and the like). However, when I try to output the results via console.log I get "undefined". When I try to parse it with "JSON.parse()" it gives me a syntax error saying that there was an "unexepected expression at line 1 column 1" of the data.
I don't understand this, especially since I used the very same script code before in other parts of the site. There, this problem never appeared.
javascript php jquery json ajax
New contributor
add a comment |
up vote
1
down vote
favorite
here's the script:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
})
}).then((data) => {
data = JSON.parse(data)
console.log("data for table is", data)
$("#requestTable").html(data);
})
}
What is returned from "getRequests.php" is markup in JSON.
Just for Reference, I'll post the backend code so you can look up what is put into the response. "$result" is what is returned at the end of the php.
<?php
/*
================================================================================
Wennn diese Datei aufgerufen wird und der Wert "ID" als Int übergeben wurde wird
eine Tabelle, die alle Anfragen anzeigt, zurückgegeben.
================================================================================
*/
if(isset($_POST["ID"])){
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
session_start();
$userRole = $connection->query("
SELECT name
FROM rolle
WHERE id = (
SELECT rolle
FROM benutzer_rollen
WHERE benutzer='".$_POST["ID"]."'
)
")->fetchAll()[0][0];
if($userRole == "admin"){
$result .= "<thead><tr><td>Von</td><td>Bis</td><td>Benutzer</td><td>Raum/Platz
</td><td>Aktionen</td></tr></thead><tbody>";
$allReservation = $connection->query("
SELECT id, anfang, ende, benutzer, arbeitsplatz
FROM reservierung
WHERE status='angefragt'
")->fetchAll();
foreach($allReservation as $row){
$user = $connection->query("
SELECT name, vorname
FROM benutzer
WHERE id='".$row["benutzer"]."'
")->fetchAll()[0];
$position = $connection->query("
SELECT raum, nummer
FROM arbeitsplatz
WHERE ID = '".$row["arbeitsplatz"]."'
")->fetchAll()[0];
$raumbild = $connection->query("
SELECT bild
FROM raum
WHERE name ='".$position["raum"]."'
")->fetchAll()[0][0];
$result .= "<tr><td>".date("d.m.y",strtotime($row["anfang"]))."</td><td>"
.date("d.m.y",strtotime($row["ende"]))."</td><td>".$user["name"]." "
.$user["vorname"]."</td><td><a>".$position["raum"]."/"
.$position["nummer"]."<div><img class="hoverImage"src="".$raumbild.
"" /></div></a></td><td><div class="form-inline form-horizontal">
<select id="statusDrop".$row["id"]."" class="form-control">
<option>genehmigen</option><option>ablehnen</option></select>
<button class='btn btn-default' onclick="submitStatus(".$row["id"].
");">Ok</button></div></td></tr>";
}
$result .= "</tbody></table>";
echo json_encode($result);
}
}
?>
In the chromeDevTools, the response is displayed correctly as HTML elements ("" and the like). However, when I try to output the results via console.log I get "undefined". When I try to parse it with "JSON.parse()" it gives me a syntax error saying that there was an "unexepected expression at line 1 column 1" of the data.
I don't understand this, especially since I used the very same script code before in other parts of the site. There, this problem never appeared.
javascript php jquery json ajax
New contributor
3
Why encoding in JSON something that is just a string and not an object/array?
– Cid
Nov 8 at 9:24
1
It's because you're not returning valid JSON. Firstconsole.log(data)
in yourthen
handler to see exactly what you are returning. It seems like you can just return the HTML and be done.
– Rory McCrossan
Nov 8 at 9:27
@RoryMcCrossan I already tried logging just the string without parsing, but then it says undefined. Also, when the markup is injected into the respective element (see last bit of code), no table appears.
– JSONBUG123
Nov 8 at 9:32
I also tried echoing without "json_encode", plus removing the JSON.parse() on the script. Didn't really change much, although in the devtools a few more results show up so "json_encode" wasn't the best idea here.
– JSONBUG123
Nov 8 at 9:35
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
here's the script:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
})
}).then((data) => {
data = JSON.parse(data)
console.log("data for table is", data)
$("#requestTable").html(data);
})
}
What is returned from "getRequests.php" is markup in JSON.
Just for Reference, I'll post the backend code so you can look up what is put into the response. "$result" is what is returned at the end of the php.
<?php
/*
================================================================================
Wennn diese Datei aufgerufen wird und der Wert "ID" als Int übergeben wurde wird
eine Tabelle, die alle Anfragen anzeigt, zurückgegeben.
================================================================================
*/
if(isset($_POST["ID"])){
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
session_start();
$userRole = $connection->query("
SELECT name
FROM rolle
WHERE id = (
SELECT rolle
FROM benutzer_rollen
WHERE benutzer='".$_POST["ID"]."'
)
")->fetchAll()[0][0];
if($userRole == "admin"){
$result .= "<thead><tr><td>Von</td><td>Bis</td><td>Benutzer</td><td>Raum/Platz
</td><td>Aktionen</td></tr></thead><tbody>";
$allReservation = $connection->query("
SELECT id, anfang, ende, benutzer, arbeitsplatz
FROM reservierung
WHERE status='angefragt'
")->fetchAll();
foreach($allReservation as $row){
$user = $connection->query("
SELECT name, vorname
FROM benutzer
WHERE id='".$row["benutzer"]."'
")->fetchAll()[0];
$position = $connection->query("
SELECT raum, nummer
FROM arbeitsplatz
WHERE ID = '".$row["arbeitsplatz"]."'
")->fetchAll()[0];
$raumbild = $connection->query("
SELECT bild
FROM raum
WHERE name ='".$position["raum"]."'
")->fetchAll()[0][0];
$result .= "<tr><td>".date("d.m.y",strtotime($row["anfang"]))."</td><td>"
.date("d.m.y",strtotime($row["ende"]))."</td><td>".$user["name"]." "
.$user["vorname"]."</td><td><a>".$position["raum"]."/"
.$position["nummer"]."<div><img class="hoverImage"src="".$raumbild.
"" /></div></a></td><td><div class="form-inline form-horizontal">
<select id="statusDrop".$row["id"]."" class="form-control">
<option>genehmigen</option><option>ablehnen</option></select>
<button class='btn btn-default' onclick="submitStatus(".$row["id"].
");">Ok</button></div></td></tr>";
}
$result .= "</tbody></table>";
echo json_encode($result);
}
}
?>
In the chromeDevTools, the response is displayed correctly as HTML elements ("" and the like). However, when I try to output the results via console.log I get "undefined". When I try to parse it with "JSON.parse()" it gives me a syntax error saying that there was an "unexepected expression at line 1 column 1" of the data.
I don't understand this, especially since I used the very same script code before in other parts of the site. There, this problem never appeared.
javascript php jquery json ajax
New contributor
here's the script:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
})
}).then((data) => {
data = JSON.parse(data)
console.log("data for table is", data)
$("#requestTable").html(data);
})
}
What is returned from "getRequests.php" is markup in JSON.
Just for Reference, I'll post the backend code so you can look up what is put into the response. "$result" is what is returned at the end of the php.
<?php
/*
================================================================================
Wennn diese Datei aufgerufen wird und der Wert "ID" als Int übergeben wurde wird
eine Tabelle, die alle Anfragen anzeigt, zurückgegeben.
================================================================================
*/
if(isset($_POST["ID"])){
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
session_start();
$userRole = $connection->query("
SELECT name
FROM rolle
WHERE id = (
SELECT rolle
FROM benutzer_rollen
WHERE benutzer='".$_POST["ID"]."'
)
")->fetchAll()[0][0];
if($userRole == "admin"){
$result .= "<thead><tr><td>Von</td><td>Bis</td><td>Benutzer</td><td>Raum/Platz
</td><td>Aktionen</td></tr></thead><tbody>";
$allReservation = $connection->query("
SELECT id, anfang, ende, benutzer, arbeitsplatz
FROM reservierung
WHERE status='angefragt'
")->fetchAll();
foreach($allReservation as $row){
$user = $connection->query("
SELECT name, vorname
FROM benutzer
WHERE id='".$row["benutzer"]."'
")->fetchAll()[0];
$position = $connection->query("
SELECT raum, nummer
FROM arbeitsplatz
WHERE ID = '".$row["arbeitsplatz"]."'
")->fetchAll()[0];
$raumbild = $connection->query("
SELECT bild
FROM raum
WHERE name ='".$position["raum"]."'
")->fetchAll()[0][0];
$result .= "<tr><td>".date("d.m.y",strtotime($row["anfang"]))."</td><td>"
.date("d.m.y",strtotime($row["ende"]))."</td><td>".$user["name"]." "
.$user["vorname"]."</td><td><a>".$position["raum"]."/"
.$position["nummer"]."<div><img class="hoverImage"src="".$raumbild.
"" /></div></a></td><td><div class="form-inline form-horizontal">
<select id="statusDrop".$row["id"]."" class="form-control">
<option>genehmigen</option><option>ablehnen</option></select>
<button class='btn btn-default' onclick="submitStatus(".$row["id"].
");">Ok</button></div></td></tr>";
}
$result .= "</tbody></table>";
echo json_encode($result);
}
}
?>
In the chromeDevTools, the response is displayed correctly as HTML elements ("" and the like). However, when I try to output the results via console.log I get "undefined". When I try to parse it with "JSON.parse()" it gives me a syntax error saying that there was an "unexepected expression at line 1 column 1" of the data.
I don't understand this, especially since I used the very same script code before in other parts of the site. There, this problem never appeared.
<?php
/*
================================================================================
Wennn diese Datei aufgerufen wird und der Wert "ID" als Int übergeben wurde wird
eine Tabelle, die alle Anfragen anzeigt, zurückgegeben.
================================================================================
*/
if(isset($_POST["ID"])){
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
session_start();
$userRole = $connection->query("
SELECT name
FROM rolle
WHERE id = (
SELECT rolle
FROM benutzer_rollen
WHERE benutzer='".$_POST["ID"]."'
)
")->fetchAll()[0][0];
if($userRole == "admin"){
$result .= "<thead><tr><td>Von</td><td>Bis</td><td>Benutzer</td><td>Raum/Platz
</td><td>Aktionen</td></tr></thead><tbody>";
$allReservation = $connection->query("
SELECT id, anfang, ende, benutzer, arbeitsplatz
FROM reservierung
WHERE status='angefragt'
")->fetchAll();
foreach($allReservation as $row){
$user = $connection->query("
SELECT name, vorname
FROM benutzer
WHERE id='".$row["benutzer"]."'
")->fetchAll()[0];
$position = $connection->query("
SELECT raum, nummer
FROM arbeitsplatz
WHERE ID = '".$row["arbeitsplatz"]."'
")->fetchAll()[0];
$raumbild = $connection->query("
SELECT bild
FROM raum
WHERE name ='".$position["raum"]."'
")->fetchAll()[0][0];
$result .= "<tr><td>".date("d.m.y",strtotime($row["anfang"]))."</td><td>"
.date("d.m.y",strtotime($row["ende"]))."</td><td>".$user["name"]." "
.$user["vorname"]."</td><td><a>".$position["raum"]."/"
.$position["nummer"]."<div><img class="hoverImage"src="".$raumbild.
"" /></div></a></td><td><div class="form-inline form-horizontal">
<select id="statusDrop".$row["id"]."" class="form-control">
<option>genehmigen</option><option>ablehnen</option></select>
<button class='btn btn-default' onclick="submitStatus(".$row["id"].
");">Ok</button></div></td></tr>";
}
$result .= "</tbody></table>";
echo json_encode($result);
}
}
?>
<?php
/*
================================================================================
Wennn diese Datei aufgerufen wird und der Wert "ID" als Int übergeben wurde wird
eine Tabelle, die alle Anfragen anzeigt, zurückgegeben.
================================================================================
*/
if(isset($_POST["ID"])){
try{
$connection = new PDO('mysql:host=localhost;dbname=arbeitsplatzverwaltung',
'verwalter','N7pY1OTl2gaBbc51');
}catch(PDOException $e){
echo $e->getMessage();
}
session_start();
$userRole = $connection->query("
SELECT name
FROM rolle
WHERE id = (
SELECT rolle
FROM benutzer_rollen
WHERE benutzer='".$_POST["ID"]."'
)
")->fetchAll()[0][0];
if($userRole == "admin"){
$result .= "<thead><tr><td>Von</td><td>Bis</td><td>Benutzer</td><td>Raum/Platz
</td><td>Aktionen</td></tr></thead><tbody>";
$allReservation = $connection->query("
SELECT id, anfang, ende, benutzer, arbeitsplatz
FROM reservierung
WHERE status='angefragt'
")->fetchAll();
foreach($allReservation as $row){
$user = $connection->query("
SELECT name, vorname
FROM benutzer
WHERE id='".$row["benutzer"]."'
")->fetchAll()[0];
$position = $connection->query("
SELECT raum, nummer
FROM arbeitsplatz
WHERE ID = '".$row["arbeitsplatz"]."'
")->fetchAll()[0];
$raumbild = $connection->query("
SELECT bild
FROM raum
WHERE name ='".$position["raum"]."'
")->fetchAll()[0][0];
$result .= "<tr><td>".date("d.m.y",strtotime($row["anfang"]))."</td><td>"
.date("d.m.y",strtotime($row["ende"]))."</td><td>".$user["name"]." "
.$user["vorname"]."</td><td><a>".$position["raum"]."/"
.$position["nummer"]."<div><img class="hoverImage"src="".$raumbild.
"" /></div></a></td><td><div class="form-inline form-horizontal">
<select id="statusDrop".$row["id"]."" class="form-control">
<option>genehmigen</option><option>ablehnen</option></select>
<button class='btn btn-default' onclick="submitStatus(".$row["id"].
");">Ok</button></div></td></tr>";
}
$result .= "</tbody></table>";
echo json_encode($result);
}
}
?>
javascript php jquery json ajax
javascript php jquery json ajax
New contributor
New contributor
edited Nov 8 at 10:07
ADyson
21.5k112441
21.5k112441
New contributor
asked Nov 8 at 9:20
JSONBUG123
63
63
New contributor
New contributor
3
Why encoding in JSON something that is just a string and not an object/array?
– Cid
Nov 8 at 9:24
1
It's because you're not returning valid JSON. Firstconsole.log(data)
in yourthen
handler to see exactly what you are returning. It seems like you can just return the HTML and be done.
– Rory McCrossan
Nov 8 at 9:27
@RoryMcCrossan I already tried logging just the string without parsing, but then it says undefined. Also, when the markup is injected into the respective element (see last bit of code), no table appears.
– JSONBUG123
Nov 8 at 9:32
I also tried echoing without "json_encode", plus removing the JSON.parse() on the script. Didn't really change much, although in the devtools a few more results show up so "json_encode" wasn't the best idea here.
– JSONBUG123
Nov 8 at 9:35
add a comment |
3
Why encoding in JSON something that is just a string and not an object/array?
– Cid
Nov 8 at 9:24
1
It's because you're not returning valid JSON. Firstconsole.log(data)
in yourthen
handler to see exactly what you are returning. It seems like you can just return the HTML and be done.
– Rory McCrossan
Nov 8 at 9:27
@RoryMcCrossan I already tried logging just the string without parsing, but then it says undefined. Also, when the markup is injected into the respective element (see last bit of code), no table appears.
– JSONBUG123
Nov 8 at 9:32
I also tried echoing without "json_encode", plus removing the JSON.parse() on the script. Didn't really change much, although in the devtools a few more results show up so "json_encode" wasn't the best idea here.
– JSONBUG123
Nov 8 at 9:35
3
3
Why encoding in JSON something that is just a string and not an object/array?
– Cid
Nov 8 at 9:24
Why encoding in JSON something that is just a string and not an object/array?
– Cid
Nov 8 at 9:24
1
1
It's because you're not returning valid JSON. First
console.log(data)
in your then
handler to see exactly what you are returning. It seems like you can just return the HTML and be done.– Rory McCrossan
Nov 8 at 9:27
It's because you're not returning valid JSON. First
console.log(data)
in your then
handler to see exactly what you are returning. It seems like you can just return the HTML and be done.– Rory McCrossan
Nov 8 at 9:27
@RoryMcCrossan I already tried logging just the string without parsing, but then it says undefined. Also, when the markup is injected into the respective element (see last bit of code), no table appears.
– JSONBUG123
Nov 8 at 9:32
@RoryMcCrossan I already tried logging just the string without parsing, but then it says undefined. Also, when the markup is injected into the respective element (see last bit of code), no table appears.
– JSONBUG123
Nov 8 at 9:32
I also tried echoing without "json_encode", plus removing the JSON.parse() on the script. Didn't really change much, although in the devtools a few more results show up so "json_encode" wasn't the best idea here.
– JSONBUG123
Nov 8 at 9:35
I also tried echoing without "json_encode", plus removing the JSON.parse() on the script. Didn't really change much, although in the devtools a few more results show up so "json_encode" wasn't the best idea here.
– JSONBUG123
Nov 8 at 9:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I think you get undefined for data
in the .then((data) => {
block because this "then" is attached to the call to getLoggedUser, not getRequests. So it receives a different response (and also probably executes before getRequests has completed).
data = JSON.parse(data)
So you need to do two things
1) Attach your callback to the correct AJAX call
2) stop trying to use JSON to try and wrap around HTML. What you're passing to json_encode() isn't JSON, and there's no need for it to be - just return the HTML as a string and append it directly to your HTML.
JS:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
}).then((data) => {
console.log("data for table is", data)
$("#requestTable").html(data);
})
})
}
PHP:
Simply replace
echo json_encode($result);
with
echo $result;
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I think you get undefined for data
in the .then((data) => {
block because this "then" is attached to the call to getLoggedUser, not getRequests. So it receives a different response (and also probably executes before getRequests has completed).
data = JSON.parse(data)
So you need to do two things
1) Attach your callback to the correct AJAX call
2) stop trying to use JSON to try and wrap around HTML. What you're passing to json_encode() isn't JSON, and there's no need for it to be - just return the HTML as a string and append it directly to your HTML.
JS:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
}).then((data) => {
console.log("data for table is", data)
$("#requestTable").html(data);
})
})
}
PHP:
Simply replace
echo json_encode($result);
with
echo $result;
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
add a comment |
up vote
0
down vote
accepted
I think you get undefined for data
in the .then((data) => {
block because this "then" is attached to the call to getLoggedUser, not getRequests. So it receives a different response (and also probably executes before getRequests has completed).
data = JSON.parse(data)
So you need to do two things
1) Attach your callback to the correct AJAX call
2) stop trying to use JSON to try and wrap around HTML. What you're passing to json_encode() isn't JSON, and there's no need for it to be - just return the HTML as a string and append it directly to your HTML.
JS:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
}).then((data) => {
console.log("data for table is", data)
$("#requestTable").html(data);
})
})
}
PHP:
Simply replace
echo json_encode($result);
with
echo $result;
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I think you get undefined for data
in the .then((data) => {
block because this "then" is attached to the call to getLoggedUser, not getRequests. So it receives a different response (and also probably executes before getRequests has completed).
data = JSON.parse(data)
So you need to do two things
1) Attach your callback to the correct AJAX call
2) stop trying to use JSON to try and wrap around HTML. What you're passing to json_encode() isn't JSON, and there's no need for it to be - just return the HTML as a string and append it directly to your HTML.
JS:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
}).then((data) => {
console.log("data for table is", data)
$("#requestTable").html(data);
})
})
}
PHP:
Simply replace
echo json_encode($result);
with
echo $result;
I think you get undefined for data
in the .then((data) => {
block because this "then" is attached to the call to getLoggedUser, not getRequests. So it receives a different response (and also probably executes before getRequests has completed).
data = JSON.parse(data)
So you need to do two things
1) Attach your callback to the correct AJAX call
2) stop trying to use JSON to try and wrap around HTML. What you're passing to json_encode() isn't JSON, and there's no need for it to be - just return the HTML as a string and append it directly to your HTML.
JS:
function getReq(){
$.post('../include/getLoggedUser.php', {
//nothing to transmit
}).then((loggedUser) => {
$.post("../include/getRequests.php", {
ID:loggedUser
}).then((data) => {
console.log("data for table is", data)
$("#requestTable").html(data);
})
})
}
PHP:
Simply replace
echo json_encode($result);
with
echo $result;
answered Nov 8 at 10:05
ADyson
21.5k112441
21.5k112441
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
add a comment |
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
Thanks, that was it! :=)
– JSONBUG123
Nov 8 at 10:10
add a comment |
JSONBUG123 is a new contributor. Be nice, and check out our Code of Conduct.
JSONBUG123 is a new contributor. Be nice, and check out our Code of Conduct.
JSONBUG123 is a new contributor. Be nice, and check out our Code of Conduct.
JSONBUG123 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204699%2fwhy-does-the-xhr-succeed-and-show-in-chromedevtools-but-is-undefined-and-cant-be%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Why encoding in JSON something that is just a string and not an object/array?
– Cid
Nov 8 at 9:24
1
It's because you're not returning valid JSON. First
console.log(data)
in yourthen
handler to see exactly what you are returning. It seems like you can just return the HTML and be done.– Rory McCrossan
Nov 8 at 9:27
@RoryMcCrossan I already tried logging just the string without parsing, but then it says undefined. Also, when the markup is injected into the respective element (see last bit of code), no table appears.
– JSONBUG123
Nov 8 at 9:32
I also tried echoing without "json_encode", plus removing the JSON.parse() on the script. Didn't really change much, although in the devtools a few more results show up so "json_encode" wasn't the best idea here.
– JSONBUG123
Nov 8 at 9:35