weixin_33694620 2015-11-27 07:26 采纳率: 0%
浏览 14

Ajax不能正常工作

this is my client side code.The server side php code is simply to echo statement.

function load(){if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari     
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.xmlhttp");}
    if(xmlhttp==null)
    { alert ("Your browser does not support XMLHttpRequest!");
        return; }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.write("Received");
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET", "http://localhost/tphp.php?", true);/*this works separately*/
    xmlhttp.send();document.write("Sent")}

My html code contains

<h4 id="txtHint">to be replaced</h4>
<input type="button" onclick="load()" value="click" />

My PHP code it contains echo statement.i just want to test it

<?php
echo "It's working";
?>
  • 写回答

2条回答 默认 最新

  • ?Briella 2015-11-27 08:24
    关注

    I am not sure if you can use jQuery, if so please make these things simple:

    $("body").append("Sent<br/>");
    $.get("/tphp.php", function (data) {
      $("body").append("Received: " + data + "<br/>");
    });
    

    Advantages of using jQuery:

    1. Code is simple.
    2. Excellent cross browser support.
    评论

报告相同问题?