无法使用PHP形式的AJAX发送GET数据

时间:2016-07-24 22:47:32

标签: javascript php ajax wolframalpha

我正在尝试修复AJAX函数以使用我的表单中的GET方法从Wolfram Alpha API检索数据,但是将数据传输到API处理程序文件,数据将传递到同一页面。 下面是代码,请告诉我我哪里出错,在这里被困3天没有任何结果: simpleRequest.php

<html>
<head>
  <meta charset="UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
function loadDoc()
{
    var xmlhttp= window.XMLHttpRequest ?
        new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            document.getElementById("demo").innerHTML = xmlhttp.responseText; // Here is the response
    }

    var query = document.getElementById('q').value;
    var queryString = "?q="+query;

    xmlhttp.open("GET","handle.php" + queryString, true);
    xmlhttp.send();
}
</script>
</head>
<body>
<form>
Search:
<input type="text" name="q" id="q" value="
<?php
  $queryIsSet = isset($_REQUEST['q']);
  if ($queryIsSet) {
    echo $_REQUEST['q'];
  };
?>"
>&nbsp;&nbsp; <input type="submit" onclick="loadDoc()" name="Search" value="Search">
</form>
<br><br>
<hr>

<div id="demo"></div>

这是处理来自Wolfram Alpha的API调用并显示结果的php文件: handle.php

<?php
  include '../wa_wrapper/WolframAlphaEngine.php';
?>
<?php  
  $appID = 'APP_ID';

  //if (!$queryIsSet) die();

  $qArgs = array();
  if (isset($_REQUEST['assumption']))
    $qArgs['assumption'] = $_REQUEST['assumption'];

  // instantiate an engine object with your app id
  $engine = new WolframAlphaEngine( $appID );

  // we will construct a basic query to the api with the input 'pi'
  // only the bare minimum will be used
  $response = $engine->getResults( $_REQUEST['q'], $qArgs);

  // getResults will send back a WAResponse object
  // this object has a parsed version of the wolfram alpha response
  // as well as the raw xml ($response->rawXML) 

  // we can check if there was an error from the response object
  if ( $response->isError() ) {
?>
  <h1>There was an error in the request</h1>
  </body>
  </html>
<?php
    die();
  }
?>

<h1>Results</h1>
<br>

<?php
  // if there are any assumptions, display them 
  if ( count($response->getAssumptions()) > 0 ) {
?>
    <h2>Assumptions:</h2>
    <ul>
<?php
      // assumptions come as a hash of type as key and array of assumptions as value
      foreach ( $response->getAssumptions() as $type => $assumptions ) {
?>
        <li><?php echo $type; ?>:<br>
          <ol>
<?php
          foreach ( $assumptions as $assumption ) {
?>
            <li><?php echo $assumption->name ." - ". $assumption->description;?>, to change search to this assumption <a href="handle.php?q=<?php echo urlencode($_REQUEST['q']);?>&assumption=<?php echo $assumption->input;?>">click here</a></li>
<?php
          }
?>
          </ol>
        </li>
<?php
      }
?>

    </ul>
<?php
  }
?>

<hr>

<?php
  // if there are any pods, display them
  if ( count($response->getPods()) > 0 ) {
?>
    <h2>Pods</h2>
    <table border=1 width="80%" align="center">
<?php
    foreach ( $response->getPods() as $pod ) {
?>
      <tr>
        <td>
          <h3><?php echo $pod->attributes['title']; ?></h3>
<?php
        // each pod can contain multiple sub pods but must have at least one
        foreach ( $pod->getSubpods() as $subpod ) {
          // if format is an image, the subpod will contain a WAImage object
?>
          <img src="<?php echo $subpod->image->attributes['src']; ?>">
          <hr>
<?php
        }
?>

        </td>
      </tr>
<?php
    }
?>
    </table>
<?php
  }
?>

</body>
</html>

现在,每当我点击搜索按钮时,GET数据都不会传递给handle.php,而是会在点击搜索按钮后将其传递给同一页面即simpleRequest.php作为url: < EM>本地主机/狼/ PHP /样品/ simpleRequest.php q = [StringToBeSearched]&安培; =搜索

请告诉我要去哪儿,请记住我是AJAX的初学者。

0 个答案:

没有答案