Good evening all.
I was hoping someone might be able to help me. I am fairly competent with JS and PHP but not so much with the fetch API which I am currently trying to get my head around.
The issue is this. If I use the .then syntaxt to handle the promises everything works fine, however if I create an async function and use the await syntax, my data is being posted to my server side script as text, not JSON, even though the headers are set correctly. Therefore, my response back from my server side script is garbage because it's expecting JSON to be sent to it. If I adjust my server side PHP script to accept text, eveything works as it should. I could change it to handle text but I would love to know what I'm doing wrong. Code is below:
[CODE lang="javascript" title="Client Side JS"]async function getDbResult(id) {
const result = await fetch('/shop_common/db_query.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(id),
});
const jsondata = await result.json();
return jsondata;
}[/CODE]
[CODE title="Server Side Code" highlight="5,8"]<?php
//Obtain the product_id from the JSON data via the php input stream
$json = file_get_contents('php://input');
$data = json_decode($json);
//Extract the value of 'product id' from the decoded JSON stream
$product_id = $data->product_id;
//Set the credentials for accessing the Db
$db_host = "**********************";
$db_user = "**********************";
$db_pass = "**********************";
$db = "**********************";
$db_table = "**********************";
$connect = mysqli_connect($db_host, $db_user, $db_pass, $db);
if ($connect -> connect_error) {
echo "Failed to connect to DB: " . $connect -> connect_error;
exit();
}
//Select all columns from the table where the product id matches the row
$sql = mysqli_query($connect, "SELECT * FROM $db_table WHERE product_id='".$product_id."'");
$result = mysqli_fetch_all($sql, MYSQLI_ASSOC);
exit (json_encode($result));
?>[/CODE]
When I pass data to the server side script using the async/await syntax, $product_id is null because it's receiving text instead of JSON.
Any help would be greatly appreciated
I was hoping someone might be able to help me. I am fairly competent with JS and PHP but not so much with the fetch API which I am currently trying to get my head around.
The issue is this. If I use the .then syntaxt to handle the promises everything works fine, however if I create an async function and use the await syntax, my data is being posted to my server side script as text, not JSON, even though the headers are set correctly. Therefore, my response back from my server side script is garbage because it's expecting JSON to be sent to it. If I adjust my server side PHP script to accept text, eveything works as it should. I could change it to handle text but I would love to know what I'm doing wrong. Code is below:
[CODE lang="javascript" title="Client Side JS"]async function getDbResult(id) {
const result = await fetch('/shop_common/db_query.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(id),
});
const jsondata = await result.json();
return jsondata;
}[/CODE]
[CODE title="Server Side Code" highlight="5,8"]<?php
//Obtain the product_id from the JSON data via the php input stream
$json = file_get_contents('php://input');
$data = json_decode($json);
//Extract the value of 'product id' from the decoded JSON stream
$product_id = $data->product_id;
//Set the credentials for accessing the Db
$db_host = "**********************";
$db_user = "**********************";
$db_pass = "**********************";
$db = "**********************";
$db_table = "**********************";
$connect = mysqli_connect($db_host, $db_user, $db_pass, $db);
if ($connect -> connect_error) {
echo "Failed to connect to DB: " . $connect -> connect_error;
exit();
}
//Select all columns from the table where the product id matches the row
$sql = mysqli_query($connect, "SELECT * FROM $db_table WHERE product_id='".$product_id."'");
$result = mysqli_fetch_all($sql, MYSQLI_ASSOC);
exit (json_encode($result));
?>[/CODE]
When I pass data to the server side script using the async/await syntax, $product_id is null because it's receiving text instead of JSON.
Any help would be greatly appreciated