не удается отправить запрос AJAX с GET - PullRequest
0 голосов
/ 06 декабря 2011
<!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>

идентификатор_потока никогда не печатается .. идентификатор_потока не задан, хотя я передал его в качестве параметра url перед отправкой в ​​коде JavaScript:

 **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 Ответ

1 голос
/ 06 декабря 2011

Этот код возвращает то, что я ожидал получить. В консоли он возвращает всю страницу. Я бы посоветовал вам прочитать о 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...