试图在单独的JS文件中调用PHP脚本,如何在HTML页面上显示输出

试图在单独的JS文件中调用PHP脚本,如何在HTML页面上显示输出,php,html,xml,Php,Html,Xml,第一次尝试这个方法时,如果有人能帮我,我会在控制台上得到一个输出,但不知道如何在标签中显示它。这是我代码的两个部分的问题。任何帮助都将不胜感激 const URL_GETOFFERS = 'getOffers.php'; const URL_GETOFFERS_XML = 'getOffers.php?useXML'; fetch(URL_GETOFFERS) .then( function (response) { return respons

第一次尝试这个方法时,如果有人能帮我,我会在控制台上得到一个输出,但不知道如何在标签中显示它。这是我代码的两个部分的问题。任何帮助都将不胜感激

const URL_GETOFFERS = 'getOffers.php';
const URL_GETOFFERS_XML = 'getOffers.php?useXML';

fetch(URL_GETOFFERS)
    .then(
        function (response) {
            return response.text();
        })
    .then(
        function (data) {
            console.log(data);
            document.getElementById("getHTMLOffer").innerHTML = "<p>" + this.responseText + "</p>";
        })
    .catch(
        function (err) {
            console.log("Something went wrong!", err);
        });

fetch(URL_GETOFFERS_XML)
    .then(
        function (response) {
            return response.text();
        })
    .then(
        function (data) {
            console.log(data);
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(data,"text/xml");
            xmlDoc.getElementsById("getXMLOffer").innerHTML = this.responseText;
        })
    .catch(
        function (err) {
            console.log("Something went wrong!", err);
        });
 });
consturl_GETOFFERS='GETOFFERS.php';
const URL_GETOFFERS_XML='GETOFFERS.php?useXML';
获取(URL\u GETOFFERS)
.那么(
功能(响应){
返回response.text();
})
.那么(
功能(数据){
控制台日志(数据);
document.getElementById(“getHTMLOffer”).innerHTML=“”+this.responseText+”

”; }) .接住( 功能(err){ log(“出错了!”,错误); }); 获取(URL\u GETOFFERS\u XML) .那么( 功能(响应){ 返回response.text(); }) .那么( 功能(数据){ 控制台日志(数据); parser=新的DOMParser(); xmlDoc=parser.parseFromString(数据,“text/xml”); xmlDoc.getElementsById(“getXMLOffer”).innerHTML=this.responseText; }) .接住( 功能(err){ log(“出错了!”,错误); }); });
如果要将某些内容放入HTML元素中,必须:

  • 创建所需的HTML元素
要知道实现这一点有多种方法。上面显示的只是其中一种方式


还要注意函数
getElementById
只获取一个元素。在您的代码中有一个输入错误
getElementsById
(额外的
s
字符)。

这是一个基本示例,它向同一页面发送一个带有查询字符串的基本GET请求,处理服务器响应并在页面上显示

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['task'] ) ){
        ob_clean();
        echo 'Some text as respons to the FETCH request task = "'.$_GET['task'].'"';
        exit();
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>FETCH</title>
    </head>
    <body>
        <aside id='getHTMLOffer'></aside>
        <script>
            fetch( '?task=fetch&source=text' )
            .then( r=>{ return r.text() } )
            .then( data=>{
                document.getElementById('getHTMLOffer').innerHTML=data
            })
            .catch(err=>{ alert( err ) } )
        </script>
    </body>
</html>

取来
获取('?任务=获取&源=文本')
.then(r=>{return r.text()})
。然后(数据=>{
document.getElementById('getHTMLOffer')。innerHTML=data
})
.catch(err=>{alert(err)})

@getset对不起,在标记中。正如我能看到的文本在控制台中输出一样。我认为当试图在中显示时,应该是
data
而不是
this.responseText
html@RamRaider我已经改变了,谢谢你,你知道我应该把什么放在HTML中显示吗?什么HTML?你能把它加到表上吗question@RamRaider我想在我的主页上的HTML标记中显示这段代码的输出。上面的代码在控制台中输出,但我不知道如何在实际页面上显示所述输出。非常感谢,这使得getHTMLOffer显示出来,有没有办法让getHTMLOffer和getXMLOffer都显示在同一页面上?
    // the contents of this function is executed after the HTML structure (DOM) is loaded
    // aka `on ready`
    (function() { 
        document.getElementById("getHTMLOffer").innerHTML = "new content here";
        document.getElementById("getXMLOffer").innerHTML = "new content here";
    })();
<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['task'] ) ){
        ob_clean();
        echo 'Some text as respons to the FETCH request task = "'.$_GET['task'].'"';
        exit();
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>FETCH</title>
    </head>
    <body>
        <aside id='getHTMLOffer'></aside>
        <script>
            fetch( '?task=fetch&source=text' )
            .then( r=>{ return r.text() } )
            .then( data=>{
                document.getElementById('getHTMLOffer').innerHTML=data
            })
            .catch(err=>{ alert( err ) } )
        </script>
    </body>
</html>