Ajax加载同一页面的内容

时间:2016-03-08 07:55:11

标签: php jquery ajax

我只是复制粘贴代码表单w3school

load_second.php中,我只需加载gethint.php,其余代码与w3school相同。

function loadDoc() {
  var xhttp;
  if (window.XMLHttpRequest) {
    // code for modern browsers
    xhttp = new XMLHttpRequest();
    } else {
    // code for IE6, IE5
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("GET", "gethint.php", true);
  xhttp.send();
}

gethint.php包含此代码<?php echo "ghjghjghj";?>

但是当我在本地主机上运行它时,它无法正常工作。

我正在使用xampp。我在本地主机上运行load_second.php,当我点击更改内容it display another Change Content button时会发生以下事情

1 个答案:

答案 0 :(得分:0)

尝试使用jQuery的Ajax。 在我看来,它更简单。

$.get( "gethint.php", function( data ) {
   $( "#demo" ).html( data );
   console.log( "Load was performed." );
});
相关问题