I have this function that is supposed to pass array to the test1.php on the server but I keep getting an error
Error while processing response: SyntaxError: Unexpected end of JSON input
I found out that this messages shows that my javascript input is ,well , not right. It occurs mostly due to the missing quotations, commas and so. However I don't see a problem with my input
mainpage.php:
function updatePHPScript() {
var passArrayJSON = JSON.stringify(passArray); // Console: {month: Array(1), number: Array(1)}
console.log("passArrayJSON:", passArrayJSON); //Console: {"month":["2018-01"],"number":["0000000000"]} - actual input that I'm trying to pass
fetch("test1.php", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: passArrayJSON
})
.then(response => response.json())
.then(data => {
console.log("Full Response Data:", data);
console.log("Response Message:", data.message);
const phoneCountElement = document.getElementById('phone-count');
// Access and display specific values from the response data
phoneCountElement.innerHTML = data.message;
})
.catch(error => {
console.error("Error while processing response:", error);
});
}
test1.php:
if (isset($_POST["passArray"])) {
$passArrayJSON = $_POST["passArray"];
$passArray = json_decode($passArrayJSON, true);
$months = $passArray["month"];
$clientNumbers = $passArray["number"];
$response = array(
"message" => "Hello from the server!"
);
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo json_encode($response);
So this is how my input looks {"month":["2018-01"],"number":["0000000000"]}
but I cannot see what is wrong with it.
I simplified my test1.php so there is no direct use of data I'm passing, but since it's in the if statement, I suppose that I should get my response message if there is not other problem