Hier ein einfaches Ajax Beispiel.
Inhalt der pdf.php
<?php echo 'foobar';
Der JavaScript Teil. In diesem Beispiel ist eine „Sofort startende Funktion“ „initCreatePdf“ vorhanden welche die init Funktion aufruft.
window.onload = function() {
let phpPath = window.location.href+'/files4u/inc/use/pdf.php';
const createPdf = {};
createPdf.init = () => {
console.log('createPdf.init() was called');
let xhr = null;
try{
xhr = new XMLHttpRequest();
}catch(e){
console.warn('Browser is not xhr compatible');
}
if (xhr) {
xhr.open('GET', phpPath, true);
xhr.onreadystatechange = function(){
if (this.readyState === 4 && this.status === 200) {
console.log('this.responseText', this.responseText);
console.log('this.response', this.response);
}
};
xhr.send(null);
}
};
// Immediately Invoke Function
(function initCreatePdf() {
console.log('initCreatePdf() was called');
createPdf.init();
})()
};