繁体   English   中英

无法从 AJAX ZF590B4FDA2C30BE28DD3C8C3CAF5C7 的 PHP API 的 GET 请求中获得响应

[英]Cannot get response from GET request to PHP API by AJAX jQuery

我正在尝试在该人使用 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')

     );

    }


I have the project running on my localhost, the "/../" means that I have index,php file in the public folder and I need to go up a level and then go to api folder where I have different folders.

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

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

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

You need to move your api folder so it has a URL (or give it a URL through some other mechanism like the Apache Alias directive).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM