在我的第一个AJAX请求中遇到麻烦

时间:2017-11-30 23:29:54

标签: 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>

1 个答案:

答案 0 :(得分:1)

将您的//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>

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

相关问题