简体   繁体   English

jQuery自动完成和AJAX组合

[英]JQuery Autocomplete and AJAX combination

I'm trying to implement auto-complete on a textbox in php. 我正在尝试在php中的文本框上实现自动完成。 The data from autocomplete is retrieved using a GET ajax call (which calls a certain api and returns json output). 使用GET ajax调用(调用某个api并返回json输出)来检索来自自动完成功能的数据。 The code for my ajax was as follows: 我的ajax的代码如下:

<script type="text/javascript"> 
$(function() {
$( "#tags" ).keyup(function() {
  var query = document.getElementsByName('newartist')[0].value;
  $.ajax({
          type: "GET",
          url: "https://lab.anghami.com/rest/v1/SEARCHedge.php",
          data: "output=jsonhp&q=" + query,
          dataType: "html",
          success: function (data) {
              var obj = JSON.parse(data);
              console.log("1. " + obj[0]);
              console.log("2. " + obj[1]);
          }
        });
      });
    });
</script>

This code was working perfectly find, and the output was shown in the console correctly. 这段代码可以完美找到,并且输出正确显示在控制台中。 Next I tried adding this ajax call as "source" to my autcomplete call as follows: 接下来,我尝试将此ajax调用作为“源”添加到我的autcomplete调用中,如下所示:

<script type="text/javascript"> 
$(function() {
var query = document.getElementsByName('newartist')[0].value;
$( "#tags" ).autocomplete({
  source: function( request, response ) {
  $.ajax({
          type: "GET",
          url: "https://lab.anghami.com/rest/v1/SEARCHedge.php",
          data: "output=jsonhp&q=" + query,
          dataType: "html",
          success: function (data) {
              var obj = JSON.parse(data);
              var outp = [];
              outp.push(obj.sections[0].data[0].name);
              outp.push(obj.sections[0].data[1].name);
              console.log(obj.sections[0].data[0].name);
              console.log(obj.sections[0].data[1].name);
              response(outp);
          }
        });
      }
      });
    });
</script>

Here, the code stopped working for some reasons, and any console.log commands I had stopped outputting the results. 在这里,由于某些原因,代码停止工作,并且我停止输出任何console.log命令的结果。

One other method I found as answer to a similar question was to implement the following code: 我发现作为回答类似问题的另一种方法是实现以下代码:

<script type="text/javascript"> 
$(function() {
$( "#tags" ).keyup(function() {
  var query = document.getElementsByName('newartist')[0].value;
  $.ajax({
          type: "GET",
          url: "https://lab.anghami.com/rest/v1/SEARCHedge.php",
          data: "output=jsonhp&q=" + query,
          dataType: "html",
          success: function (data) {
              var obj = JSON.parse(data);
              var artists = [];
              artists.push(obj[0]);
              artists.push(obj[1]);
              test(obj);
          }
        });
      });
    });
   function test(data){ 
   console.log(data);
   $("#tags").autocomplete({
        source: data
    });
   }
</script>

This was a bit better as autocomplete was indeed suggesting results, but it was inconsistent as it sometimes outputted 1 result instead of 2 (my ajax call ALWAYS returns 2 results, and I made sure that it's always the case by using console.log), and sometimes the suggestions proposed by autocomplete were those of the previous AJAX call and not the current one (again, console showed new results but autocomplete suggested previous ones. 由于自动完成功能确实在暗示结果,所以它要好一些,但是由于有时输出1个结果而不是2个结果(我的ajax调用ALWAYS返回2个结果,并且我使用console.log始终确保是这种情况),因此不一致有时自动完成建议的建议是先前AJAX调用的建议,而不是当前建议(再次,控制台显示了新结果,但自动完成建议了先前的建议。

If you could point to the errors in both of these methods, it would be great. 如果您可以指出这两种方法中的错误,那就太好了。 Thanks!! 谢谢!!

Code looking fine. 代码看起来不错。 Please mention the JSON output format. 请提及JSON输出格式。

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

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