[英]Cannot get response from GET request to PHP API by AJAX jQuery
I am trying to get the data while the person inputs the text using a "keyup" event in jQuery.我正在尝试在该人使用 jQuery 中的“keyup”事件输入文本时获取数据。
I'm actually searching in database to find all the words which match with pattern.我实际上是在数据库中搜索以查找与模式匹配的所有单词。 However, I'm not getting the response from PHP API.
但是,我没有收到 PHP API 的响应。 The error is
错误是
404 - Not found.
404 - 未找到。
I am new in AJAX jQuery and I'm trying to understand where is could be the problem.我是 AJAX jQuery 的新手,我试图了解问题出在哪里。 Also, I tested the PHP file with Postman and it works, returning json format data.
另外,我用 Postman 测试了 PHP 文件,它可以工作,返回 json 格式数据。 So, I assume that the problem is with my Jquery code.
所以,我认为问题出在我的 Jquery 代码上。
There is my AJAX jQuery where I'm sending input value.有我的 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>
This is my PHP file which I'm calling.这是我正在调用的 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.
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.
You can only access a PHP program if it has a URL.如果 PHP 程序具有 URL,则您只能访问它。
Assuming your public folder is the DocumentRoot, then any files outside that directory do not have URLs .假设您的公用文件夹是 DocumentRoot,那么该目录之外的任何文件都没有 URL 。
You can't send ..
to an HTTP server and access arbitrary files on it.您不能将
..
发送到 HTTP 服务器并访问其上的任意文件。 Imagine if people were able to use ../../../../../etc/passwd
to collect the password files of any server.想象一下,如果人们能够使用
../../../../../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). 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.