Ajax和PHP请求页面并动态显示

Ajax和PHP请求页面并动态显示,php,javascript,ajax,Php,Javascript,Ajax,我有一个Ajax文件,我正试图与一个PHP文件进行通信,该文件将提供Google主页,我在同一页面的一些HTML中有一个DIV 我已经彻底检查了代码和PHP文件,没有发现任何错误,但我不明白为什么什么都没有发生。谁能帮帮我吗?。提前谢谢 这是我的ajax <html> <head> <title>Ajax page</title> </head> <body> <h1>AJAX pag

我有一个Ajax文件,我正试图与一个PHP文件进行通信,该文件将提供Google主页,我在同一页面的一些HTML中有一个DIV

我已经彻底检查了代码和PHP文件,没有发现任何错误,但我不明白为什么什么都没有发生。谁能帮帮我吗?。提前谢谢

这是我的ajax

<html>
  <head>
    <title>Ajax page</title>
  </head>
    <body> 

<h1>AJAX page</h1>
  <div id="info">
    This content will be changed by default....
  </div>

<script type="text/javascript">

params = "url=google.com";
request = new ajaxRequest();
request.open("POST", "ajaxLab.php", true);

request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");

request.onreadystatechange = function(){
  if(this.readystate==4){
    if(this.status==200){
      if(this.responseText != null) document.getElementById("info").innerHTML=this.responseText;
      else alert("Ajax error: No data received ");
    }
    else alert("Ajax error: " + this.statusText);
  }
}
request.send(params);

function ajaxRequest()
{
  try
  {
  var request = new XMLHttpRequest();
  }
  catch(e1)
  {
    try
    {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e2)
    {
      try
      {
      request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e3)
      {
      request=false;
      }
    }
  }
return request; 
} 

</script>

  </body>
</html>

Ajax页面
AJAX页面
默认情况下,此内容将被更改。。。。
params=“url=google.com”;
请求=新的ajaxRequest();
open(“POST”,“ajaxLab.php”,true);
setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
setRequestHeader(“内容长度”,参数长度);
setRequestHeader(“连接”、“关闭”);
request.onreadystatechange=函数(){
if(this.readystate==4){
if(this.status==200){
如果(this.responseText!=null)document.getElementById(“info”).innerHTML=this.responseText;
else警报(“Ajax错误:未收到数据”);
}
else警报(“Ajax错误:+this.statusText”);
}
}
请求发送(params);
函数ajaxRequest()
{
尝试
{
var request=new XMLHttpRequest();
}
渔获物(e1)
{
尝试
{
请求=新的ActiveXObject(“Msxml2.XMLHTTP”);
}
渔获物(e2)
{
尝试
{
请求=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
渔获物(e3)
{
请求=假;
}
}
}
返回请求;
} 
这是我的简单php文件

<?php

if(isset($_POST['url'])){
echo file_get_contents("http://".sanitiseVar($_POST['url']));
}

function sanitiseVar($var){
    $var = htmlentities($var);
    $var = stripslashes($var);
    return strip_tags($var);
}

?>

通过以下方式更改整个文件:

<html>
    <head>
        <title>Ajax page</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#info").load("ajaxLab.php", {'url': 'google.com'});
            });
        </script>
    </head>
    <body> 
        <h1>AJAX page</h1>
        <div id="info">
            This content will be changed by default....
        </div>
    </body>
</html>

Ajax页面
$(文档).ready(函数(){
$(“#info”).load(“ajaxLab.php,{'url':'google.com});
});
AJAX页面
默认情况下,此内容将被更改。。。。

通过以下方式更改整个文件:

<html>
    <head>
        <title>Ajax page</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#info").load("ajaxLab.php", {'url': 'google.com'});
            });
        </script>
    </head>
    <body> 
        <h1>AJAX page</h1>
        <div id="info">
            This content will be changed by default....
        </div>
    </body>
</html>

Ajax页面
$(文档).ready(函数(){
$(“#info”).load(“ajaxLab.php,{'url':'google.com});
});
AJAX页面
默认情况下,此内容将被更改。。。。
使用以下方法:

$(document).ready(function(){
    $.post("ajaxLab.php",
           {
                url:"google.com",
                // another_param : "its value"
           },
           function(result){ // Callback function, on success
             if(result != null)
                 $('#info').html(result);
             else
                alert("Ajax error: No data received ");
          }
    );    
});
请记住在标题中包含jQuery库:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

使用以下方法:

$(document).ready(function(){
    $.post("ajaxLab.php",
           {
                url:"google.com",
                // another_param : "its value"
           },
           function(result){ // Callback function, on success
             if(result != null)
                 $('#info').html(result);
             else
                alert("Ajax error: No data received ");
          }
    );    
});
请记住在标题中包含jQuery库:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>



好的,readyState属性没有颠簸循环。在我纠正了那件事之后工作了

好的,readyState属性没有颠簸capped。在我纠正了那件事之后工作了

你能解释一下“什么都没发生”是什么意思吗?PHP页面是否正常工作?你试过直接访问它吗?你有错误吗?等等…你能用jQuery吗?代码量会简单得多!我访问HTML页面,希望Google主页出现在我的DIV信息中。据我所知,PHP只是与AJAX异步通信,在服务器接收到页面后,将其发送到我的浏览器。我是Ajax新手。我要做的第一件事是尝试找出问题是在PHP还是JavaScript中。也许可以尝试将
this.responseText
替换为
'$'+this.responseText+'$'
,以查看该行是否正在运行。还可以通过在PHP脚本中回显
$\u POST['url']
变量来检查PHP是否接收到了您认为它正在接收的内容。@ColorWP.com有些人喜欢理解他们的“魔力”在做什么。您能解释一下“什么都没发生”的意思吗?PHP页面是否正常工作?你试过直接访问它吗?你有错误吗?等等…你能用jQuery吗?代码量会简单得多!我访问HTML页面,希望Google主页出现在我的DIV信息中。据我所知,PHP只是与AJAX异步通信,在服务器接收到页面后,将其发送到我的浏览器。我是Ajax新手。我要做的第一件事是尝试找出问题是在PHP还是JavaScript中。也许可以尝试将
this.responseText
替换为
'$'+this.responseText+'$'
,以查看该行是否正在运行。还可以通过在PHP脚本中回显
$\u POST['url']
变量来检查PHP是否接收到了您认为它正在接收的内容。@ColorWP.com有些人喜欢了解他们的“魔力”在做什么。@Gareth在“纯”javascript中无法实现AJAX(即没有一些库)是的,跨域Ajax是一个安全问题-它使用PHP模块异步获取文件。@PraveenKumar当OP向问题添加[jquery]或您回答问题时,我将删除我的-1。@Gareth无法在“纯”javascript(即没有一些库)中执行Ajax是,跨域Ajax是一个安全问题-它使用PHP模块异步获取文件。@PraveenKumar我将在OP向问题添加[jquery]或您回答问题时删除我的-1。