无法使用GET发送ajax请求

时间:2011-12-06 14:19:06

标签: php ajax xmlhttprequest

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="untitled.js" type="text/javascript"></script>
<script type="text/javascript" src="/local/path/to/firebug-lite.js"></script>
<script type="text/javascript">
  var myRequest;
  var serverAddress="<?php echo $_SERVER['SCRIPT_NAME']; ?>?thread_id="+4;

  function doWork()
  {
      myRequest=createXmlHttpRequestObject();
      if(myRequest!=null)
      {
         try
         {
           myRequest.open("GET",serverAddress,true);
          myRequest.onreadystatechange=display;
           myRequest.send(null);
         }
         catch(e)
         {
             alert("Send failed");
         }
      }
  }


  function display()
  { 
      if(myRequest.readyState ==4)
      {
          if(myRequest.status==200)
          {


                  getData();

          }
      }
      else
      {

      }

  }

  function getData()
  {

  }

</script>
</head>

<body>


<?php 

  if( isset($_GET["thread_id"]) )
  { 
       echo 'Thread_id: '.$_GET["thread_id"];
  }


  /*     $dom=new DOMDocument();
     $response=$dom->createElement('response');
     $dom->appendChild($response);
     $responseText=$dom->createTextNode('1dd');
     $response->appendChild($responseText);
     $xmlString=$dom->saveXML();
     echo $xmlString;*/


?>




<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<form action="" method="get">

<table width="200" border="1" align="center">
  <tr>
    <td>
    Name:
    </td>
    <td>
    <input name="name" type="text" />
    </td>
  </tr>


  <tr>
    <td>
    Password:
    </td>
    <td>
    <input name="password" type="text" />
    </td>
  </tr>


  <tr>
    <td colspan="2" align="center">  
    <input name="post" type="button" value="post"  onclick="doWork()"/>
    </td>


  </tr>
</table>

</form>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div align="center" id="myDiv2"></div>


</body>
</html>

Thread_id永远不会被打印..没有设置thread_id,即使我在javascript代码中发送它之前把它作为url参数:

 **if( isset($_GET["thread_id"]) )


 { 
       echo 'Thread_id: '.$_GET["thread_id"];
  **}**

为什么trhead_id没有回显,当我点击按钮时(当获取xmlhttpRequest时?!?)**

Source:http://www.coursesweb.net/ajax/ajax-get-php
I want to to get $_Get..The question is whether I can send a request to my own page or I have to send a request with the Get parameters to another page...to make an echo!??!
    <?php
    // if data are received via GET, with index of 'test'
    if (isset($_GET['test'])) {
        $str = $_GET['test'];             // get data
        echo "The string '<i>".$str."</i>' contains ". strlen($str). ' characters and '. str_word_count($str, 0). ' words.';
    }
    ?> 

1 个答案:

答案 0 :(得分:1)

此代码返回我预期返回的内容。在控制台中,它返回整个页面。我建议你阅读XHR和AJAX。

可能是开始http://www.w3schools.com/ajax/default.asp

的好地方
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="untitled.js" type="text/javascript"></script>
<script type="text/javascript" src="/local/path/to/firebug-lite.js"></script>
<script type="text/javascript">
  var myRequest;
  var serverAddress="<?php echo $_SERVER['SCRIPT_NAME']; ?>?thread_id="+4;

  function doWork()
  {
      myRequest=new XMLHttpRequest();
      if(myRequest!=null)
      {
         try
         {
           myRequest.open("GET",serverAddress,true);
          myRequest.onreadystatechange=display;
           myRequest.send(null);
         }
         catch(e)
         {
             alert("Send failed");
         }
      }
  }


  function display()
  { 
      if(myRequest.readyState ==4)
      {
          if(myRequest.status==200)
          {

                console.debug(myRequest.responseText);
                  getData();

          }
      }
      else
      {

      }

  }

  function getData()
  {

  }

</script>
</head>

<body>


<?php 

  if( isset($_GET["thread_id"]) )
  { 
       echo 'Thread_id: '.$_GET["thread_id"];
  }


  /*     $dom=new DOMDocument();
     $response=$dom->createElement('response');
     $dom->appendChild($response);
     $responseText=$dom->createTextNode('1dd');
     $response->appendChild($responseText);
     $xmlString=$dom->saveXML();
     echo $xmlString;*/


?>




<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<form action="" method="get">

<table width="200" border="1" align="center">
  <tr>
    <td>
    Name:
    </td>
    <td>
    <input name="name" type="text" />
    </td>
  </tr>


  <tr>
    <td>
    Password:
    </td>
    <td>
    <input name="password" type="text" />
    </td>
  </tr>


  <tr>
    <td colspan="2" align="center">  
    <input name="post" type="button" value="post"  onclick="doWork()"/>
    </td>


  </tr>
</table>

</form>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div align="center" id="myDiv2"></div>


</body>
</html>