preview get type of node
<?php
// Retrieve the current URL
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// Check if the URL contains the specific substring
if (strpos($url, '/node/preview/') !== false) {
// If URL contains '/node/preview/', process it to create a new URL
$url_array = explode('/node/preview/', $url);
$url_array = explode('/', $url_array[1]);
// Construct the final URL
$url_final = "http://localhost/music_player/web/jsonapi/node/article/" . $url_array[0] . "/preview";
// Redirect to the constructed URL
// header('Location: ' . $url_final);
} else {
$url_array = explode('/node/', $url);
$url_final = "http://localhost/music_player/web/anynode/" . $url_array[1] . "/?_format=json";
}
?>
<div id="temp"></div>
<script>
// Function to fetch data from an API
function fetchData(url) {
fetch(url)
.then(response => {
// First, handle the HTTP response status
if (!response.ok) {
throw new Error('Network response was not ok: ' + response.statusText);
}
// Then, read and parse the JSON data
console.log( response.text );
return response.json();
})
.then(data => {
// Handle the JSON data here
console.log('Success:', data);
try{
document.getElementById('temp').innerHTML = data.data.type;
}catch(e){
document.getElementById('temp').innerHTML = data[0].type[0].target_id;
}
})
.catch(error => {
// Handle any errors here
console.error('Error:', error);
});
}
// Example API URL
const apiUrl = '<?php echo $url_final; ?>';
// Call the function with the API URL
fetchData(apiUrl);
</script>
留言
發佈留言