首页 > 解决方案 > 无法通过 AJAX jQuery 从 GET 请求中获得对 PHP API 的响应

问题描述

我试图在用户使用 jQuery 中的“keyup”事件输入文本时获取数据。

我实际上是在数据库中搜索以查找与模式匹配的所有单词。但是,我没有收到来自 PHP API 的响应。错误是

404 - 未找到。

我是 AJAX jQuery 的新手,我试图了解问题出在哪里。另外,我用 Postman 测试了 PHP 文件,它可以正常工作,返回 json 格式的数据。所以,我认为问题出在我的 Jquery 代码上。

我在 AJAX jQuery 中发送输入值。

var searchRequest = null;
$(function() {
  var minlength = 3;

  $("#phrEn").keyup(function() {
    var lang = $('option:selected', "#lang"),
      value = $('option:selected', "#lang").attr('value');
    var that = this,
      value1 = $(this).val();
    if (value.length >= minlength) {
      if (searchRequest != null)
        searchRequest.abort();
      searchRequest = $.ajax({

        type: "GET",
        url: "/../api/english/createPhrase.php",
        data: {
          'tablename': value,
          'pattern': value1
        },
        dataType: "json",
        success: function(data) {

          $.each(data, function(key, value) {
            $("#result").append(value);
          });
        },
        complete: function() {

        }

      });
    }

  });

});
<form action="" method="POST" id="formid">
  <h3>My first dictionary!</h3>
  <div class="label">
    <label>Translate from English</label>
  </div>
  <div class="enform">
    <textarea class="form" id="phrEn" name="enPhrase" placeholder="Type phrase on English language" cols="50" rows="10" wrap="hard"></textarea>
  </div>
  <div class="addbutton">
    <button class="button" type="submit" id="btnAdd" name="submit" onclick="insertIntoDatabase();">Add</button>
  </div>
  <div class="selectlist">
    <div class="dropdown" style="float:center">

      <div class="dropdown-content">
        <label>To</label>
        <select name="language" id="lang">
          <option value="spanish">Spanish</option>
          <option value="french">French</option>
          <option value="german">German</option>
          <option value="russian">Russian</option>
        </select>
      </div>
    </div>
  </div>
  <div class="otherform">
    <textarea class="form" id="phr" name="Phrase" placeholder="Translatated phrase on other language" cols="50" rows="10" wrap="hard"></textarea>
  </div>
  <br>
  <button class="button" type="submit" name="submit">Translate</button>
</form>
</div>
<div class="margin2">
  <span><p id="result"></p></span>
</div>

这是我正在调用的 PHP 文件。

    <?php 
//Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorisation, X-Requested-With');

include_once '../../config/dbh.php';
include_once '../../model/english.php';
include_once '../../model/spanish.php';
include_once '../../model/french.php';
include_once '../../model/german.php';
include_once '../../model/russian.php';

$database = new Dbh();
$db = $database->connect();

    $tablename = $_GET['tablename'];

    $pattern = $_GET['pattern'];

    $query = 'SELECT english.phrase, '.$tablename.'.phraseSp

    FROM english

    LEFT JOIN '.$tablename.' ON english.id = '.$tablename.'.english_id

    WHERE english.phrase LIKE "'.$pattern.'%"';

    $stmt = $db->prepare($query);
    $result = $stmt->execute([$tablename, $tablename, $tablename, $pattern]);         
    $num = $stmt->rowCount();

    $lEnglish = new English($db);

    $lSpanish = new Spanish($db);

    $phrEn = $lEnglish->phrase;

    $phr = $lSpanish->phrase;

    if($num > 0) {

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($rows);

    } else {

    echo json_encode(

    array('message' => 'No phrases found')

     );

    }


标签: phpjqueryajaxgetresponse

解决方案


我的项目在我的本地主机上运行,​​“/../”表示我在公共文件夹中有 index,php 文件,我需要升级一个级别,然后转到我有不同文件夹的 api 文件夹。

如果 PHP 程序有 URL,您只能访问它。

假设您的公用文件夹是 DocumentRoot,那么该目录之外的任何文件都没有 URL

您不能发送..到 HTTP 服务器并访问其上的任意文件。想象一下,如果人们能够用来../../../../../etc/passwd收集任何服务器的密码文件。

您需要移动您的api文件夹,使其具有 URL(或通过 ApacheAlias指令等其他机制为其提供 URL)。


推荐阅读