javascript - 我的第一个 AJAX 请求遇到了麻烦

标签 javascript php jquery html ajax

嘿大家,我正在尝试通过按钮触发器使用 AJAX 发送请求,并在跨度区域中获取响应 html 文件,我不知道问题出在哪里,我尝试了一些方法,但没有结果,请检查我的代码:

//ajax.php

<html>

<head>
    <script type="text/javascript">
        function ajax() {
            var xmlhttp;

            //check the xml object comptbl
            if (window.xmlHttpRequest) 
                xmlhttp = new XMLHttpRequest();
             else 
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

            xmlhttp.onreadystatechange = function()
            {
            //check the request and make the response 
            if (xmlhttp.readyState == 4 & xmlhttp.status == 200) 

                    document.getElementById("ajax").innerHTML = xmlhttp.responseText;

            }
            xmlhttp.open("GET", "info.html", true);
            xmlhttp.send();


        }

    </script>
</head>

<body>
<span id="ajax">

    <!--response area -->
</span>

<button type="button" onclick="ajax()">make the request</button>


</body>


</html>

//info.html

   <html>

    <head>
        <title>the response file</title>
    </head>
    <style>
        h1,h3 {
            color: green;
     font-family : 'ALGERIAN';

        }



    </style>

    <body>

        <h1>WELCOME IM THE RESPONSE FROM THE SERVEUR</h1>
        <h3>WITHOUT RELOAD THE WHOLE PAGE</h3>


    </body>

    </html>

最佳答案

将//info.html 更改为此

<style>
    h1,h3 {
        color: green;
 font-family : 'ALGERIAN';
    }
</style>


    <h1>WELCOME IM THE RESPONSE FROM THE SERVEUR</h1>
    <h3>WITHOUT RELOAD THE WHOLE PAGE</h3>

如果这不起作用,请发送简单的文本回复。希望这会起作用

关于javascript - 我的第一个 AJAX 请求遇到了麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47584336/

相关文章:

javascript - 如何清除多选框数据

javascript - 使用 background-size :contain 时获取 div 背景图像的 XY 坐标和高度/宽度

php - Guzzle 返回 cURL 错误 3 : <url> malformed

php - 如何在动态网页中使用名称而不是 id

javascript - jspdf 创建空 pdf

Javascript计算 - 无法让数字减少,只能增加

javascript - 如何使用 react 钩子(Hook)实现多个复选框

javascript - 如何异步多次调用 promise 函数以用给定数量的值填充数组?

php - 选择选项中的 zend 表单自定义属性?

javascript - 用javascript替换字符串中的多次出现