Ajax 数据嵌入页面,而不是返回正确的结果
Ajax data embedding page instead of returning proper results
我有一个简单的搜索区域,它使用JavaScript在用户键入时显示相关的搜索结果。以下是它的代码:
<div class="message_content">
<input type="text" id="user_search" class="form-control search_box" placeholder="Search for people">
<div id="search_results">
<span class="no-result" style="line-height: 38px;">Enter your query</span>
</div>
</div>
以下是lib.js
中的 JS:
$("input#user_search").keyup(function() {
var elt = $("input#user_search").val();
$curr = $(this);
$.ajax({
url : "u.search.php",
type: "GET",
data : {val: elt},
success:function(data, textStatus, jqXHR)
{
if (data.length) {
$("div#search_results").html(data);
}
else $("div#search_results").html('<span class='"no-result'">Enter your query</span>');
},
error: function(jqXHR, textStatus, errorThrown){}
});
});
最后,以下是u.search.php
的代码:
<?php
$search = strtolower(htmlspecialchars($_GET['val']));
if (strlen($search) < 1);else {
// Run query on the DB
...
print "<div id='"search-list'" class='"list-group'">";
// populate results
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
// variable declarations
print "<a href='"#'" id='"$result'" class='"list-group-item'">";
print "<img src='"$pic'">";
print "<span>$name</span>";
print "</a>";
}
print "</div>";
}
?>
这里的问题是,如果我在文本框、index.php
页面(搜索框所在的页面)中键入任何内容,div#search_results
的 html 将变为index.php
.如果我在 ajax success
函数中console.log(data)
,控制台会显示 index.php 页面的代码(不是实际的源代码,而是您可以在浏览器的页面源代码中看到的代码)。
div#search_results
基本上有索引.php嵌入其中。我尝试对 PHP 后端代码进行更改,但结果中没有反映任何更改。这是怎么回事?
您正在使用查询字符串来获取搜索变量。但是你没有发送任何东西。 因此,更改搜索.php使用 POST 或更改 ajax 方法来发送查询字符串...
$.ajax({
url : "u.search.php?val=" + encodeURIComponent(elt),
type: "GET",
success:function(data, textStatus, jqXHR)
{
if (data.length) {
$("div#search_results").html(data);
}
else $("div#search_results").html('<span class='"no-result'">Enter your query</span>');
},
error: function(jqXHR, textStatus, errorThrown){}
});
相关文章:
- JSONP请求返回结果,但也触发error_callback
- async问题,JS Promise无法返回结果,但可以使用console.log
- Meteor js-控制台显示'未定义'即使返回结果
- 正在等待HTTP调用从函数返回结果
- 使用函数for循环遍历对象以更改值,然后返回结果
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- Javascript 函数不返回结果
- 用逗号分隔返回结果中的对象
- 需要呈现MongoDB查询返回结果的特定索引/位置
- HTML5 文件阅读器如何返回结果
- javascript循环遍历列表项,并在数组中返回结果
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- 在angularjs工厂中,如何在函数解析后返回结果
- 我在jQuery中的Rock Paper Scissors程序只在平局时返回结果
- 如何使用ajax调用php并返回结果
- 添加第二维度的Javascript未返回结果
- Node.js没有返回结果
- jquery裁剪图像selction未返回结果
- 如何在phonegap javascript函数中返回结果
- 如果文本筛选器在ng重复中未返回结果,则显示消息