[英]JQuery Ajax $_GET request received an incorrect response from PHP
[英]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.