Hello,
I am trying to get the data from the current page to use it in my JS.
The site structure looks like this:
- Home
- Projects
- Project 1
- Project 2
- Project 3
- About
So basically I’ve create the file “project.json.php” where I am sending the title as a test:
<?php
$data = array(
'title' => (string)$page->title()
);
echo json_encode($data);
?>
And getting the retrieving the data from the “project.js” file
fetch('./project.json ', {
method: 'GET',
headers: {
'X-Requested-With': 'fetch'
}
})
.then(function(response) {
console.log(response);
return response.json();
})
But I am getting 404 error because it is accessing “…/projects/project.json” and doesn’t exist. I am only able to get the data if I change manually the name of the URL in the “project.js” fetch to the name of the post ('./project-example.json')
but that makes it less useful since I want to get the json data in my project.js for every post.
How can I get the data for each project I am in through “project.js”?