jQuery UI自动完成不使用Wordpress

时间:2015-05-23 02:15:06

标签: php jquery wordpress jquery-ui autocomplete

我想在Wordpress上使用jQuery UI Autocomplete,但由于某种原因它无法正常工作。

无论如何,我会告诉你我已经拥有的东西:

HTML

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content=
"Search the world's information, including webpages, images, videos and more. Google has many special features 
to help you find exactly what you're looking for." name="description"><meta content="noodp" name="robots">
<meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title><script>(function(){window.google={kEI:'SAxgVa73L4_FgwSy34LgCw',kEXPI:'3700256,3700366,4017578,4026111,4029815,4031300,4032032,4032500,4032521,
4032631,4032643,4032645,4032677,4032926,4033142,4033184,4033191,4033307,4033344,4034425,4035816,4035881,4035980,
4036005,4036345,4036464,4036486,4036531,4036539,4036665,4036896,4037457,4037538,4037611,8300096,8500394,8500851,
8501248,8501279,8501295,8501351,8501406,8501489,8501497,10200083,10201180,10201191',authuser:0,kSID:'c9c918f0_10'};
google.kHL='en';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)
a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=
a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){};google.time=
function(){return(new Date).getTime()};google.log=function(a,b,e,f,l){var d=new Image,h=google.lc,g=google.li,c="",m=
google.ls||"";d.onerror=d.onload=d.onabort=function(){delete h[g]};h[g]=d;if(!e&&-1==b.search("&ei=")){
var k=google.getEI(f),c="&ei="+k;-1==b.search("&lei=")&&((f=google.getLEI(f))?c+="&lei="+f:k!=google.kEI&&(
c+="&lei="+google.kEI))}a=e||"/"+(l||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+c+m+"&zx="+google.time();/^
http:/i.test(a)&&google.https()?(google.ml(Error("a"),!1,{src:a,glmm:1}),delete h[g]):(window.google&&window.google.vel&&

的JavaScript

<input type="text" name="db-search" id="db-search" autocomplete="off" />

PHP (autocomplete.php)

$('#db-search').autocomplete({ 
    source: function (request, response) {
        $.ajax({
      type: "POST",
      url:"/wp-content/themes/your-click/autocomplete.php",
      data: { autocomplete: 'true' },
    });
      }
    }, { minLength: 1 });

使用chrome测试时,我没有收到任何错误。所以我不知道我的代码有什么问题,但我猜想PHP必定有问题。

1 个答案:

答案 0 :(得分:0)

更新

问题上的

php似乎在期待&#34; GET&#34;请求$_GET['term']?尝试将$string = wpdb::_real_escape( $_POST['autocomplete'] );替换为$string = wpdb::_real_escape( $_GET['term'] );

请参阅Autocomplete - Remote JSONP datsource - viewsource

.autocomplete()预计response(Array),

上似乎也是逗号data: { autocomplete: 'true' },

尝试

$('#db-search').autocomplete({ 
    source: function (request, response) {
              // input query 
              var term = request.term;
              $.ajax({
                type: "POST",
                url:"/wp-content/themes/your-click/autocomplete.php",
                // "POST" `term` to server
                data: { autocomplete: term }
              }).then(function(data) {
                response(data)
              }, function error(jqxhr, textStatus, errorThrown) {
                console.log(textStatus, errorThrown)
              });
      }
    },
    minLength: 1 
});