2016-11-14 74 views
1

我是使用ajax的新手。我有一个Ajax程序,当用户在搜索栏中查找用户时,为用户进行实时搜索。它的功能是在每个键上使用ajax从数据库中提取数据以进行推荐以帮助用户选择。 但这个问题对于大多数页面来说都很好,除了编辑和显示特定数据的页面。ajax不适用于网址为l的网页吗?

这里是AJAX

function suser(str){ 

     var xhttp; 
     if(str.length==0) 
     { document.getElementById("srtd").innerHTML = "" 
      document.getElementById("srtd").style.border="0px"; 
      return;} 
     else{ 
     if (window.XMLHttpRequest) { 
     xhttp = new XMLHttpRequest(); 
     } else { 
     // code for IE6, IE5 
     xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xhttp.onreadystatechange = function() { 
     if (xhttp.readyState == 4 && xhttp.status == 200) { 
      var res=xhttp.responseText; 
      var i=JSON.parse(res); 
      var j=i.length; 
      document.getElementById("srtd").innerHTML = ""; 
      var d=document.getElementById("srtd"); 
     var u=document.createElement("ul"); 
     u.style="list-style-type:none"; 

      d.appendChild(u); 
      for(var k=0; k<j ; k++){ 
     var li=document.createElement("li"); 

     u.appendChild(li); 
      li.innerHTML="<a href='livsearres/"+ i[k].id +"' class='btn' >"+ i[k].name +" "+i[k].fname+" "+i[k].gname+"</a>"; 

      } 
      //document.getElementById("srtd").innerHTML="<a href='showuser/"+ i[0].id +"' class='btn mybtn-n' >"+ i[0].name +" "+i[0].fname+"</a>"; 


     } 
     } 
     xhttp.open("GET", "lusrser/"+str, true); 
     xhttp.send();} 
    } 

这是我的控制器返回用户为每一个页面上的AJAX

public function livesearch($str) 
{ 

    $users=User::where('name', 'LIKE', $str.'%')->orWhere('userid', 'LIKE', $str.'%')->get(); 
    $searchs = explode(" ", $str); 

    if (count($searchs) == 1) 
    { 
    $users=User::where('name', 'LIKE', $str.'%')->orWhere('userid', 'LIKE', $str.'%')->get(); 
      return $users; 
     } 
     elseif (count($searchs) == 2) 
    { 
    $users=User::where('name', 'LIKE', $searchs[0].'%')->where('fname', 'LIKE', $searchs[1].'%')->get(); 
      return $users; 
     } 
     elseif (count($searchs) == 3) 
    { 
    $users=User::where('name', 'LIKE', $searchs[0].'%')->where('fname', 'LIKE', $searchs[1].'%')->where('gname', 'LIKE', $searchs[2].'%')->get(); 
     return $users; 
     } 
     else 
    { 
     $users="[]"; 
    return $users; 
     } 

} 

有搜索输入类似

<input onkeyup="suser(this.value)" type="search" name="search" class="form-control" placeholder="Search" > 

网址是

route::get('/indexuser','[email protected]'); 
route::get('/createuser','[email protected]'); 
route::post('/createuser','[email protected]'); 
route::get('/edituser/{id}','[email protected]'); 
route::post('/edituser','[email protected]'); 
route::get('/showuser/{id}','[email protected]'); 

因为只有showuser和edituser不工作,我认为它与被编辑或显示任何机构可以帮助我如何解决它的ID的附加数据做。

错误消息

Sorry, the page you are looking for could not be found. 

1/1 
NotFoundHttpException in RouteCollection.php line 161: 
in RouteCollection.php line 161 
at RouteCollection->match(object(Request)) in Router.php line 821 
at Router->findRoute(object(Request)) in Router.php line 691 
at Router->dispatchToRoute(object(Request)) in Router.php line 675 
at Router->dispatch(object(Request)) in Kernel.php line 246 
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 
at Pipeline->then(object(Closure)) in Kernel.php line 132 
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 
at Kernel->handle(object(Request)) in index.php line 54 
+0

你在xhr(控制台)中遇到什么错误?我猜这个问题在'xhttp.open(“GET”,“lusrser /”+ str,true);'。它没有击中你想要的URL,因为你已经在你的url中获取参数。 –

+0

我认为你是正确的,因为它显示路由未找到,这意味着URL不正确。我该如何解决这个问题。 – user3266023

+0

检查我的答案哥们。我想这会帮助你理解这个问题。 –

回答

1

如果您使用的是刀片文件,您可以使用这一招。

定义一个像这样的JavaScript变量。

var SiteUrl = '{{ url::to("") }}'; 

而且你可以用你的ajax url这样连接这个。

xhttp.open("GET", SiteUrl+"lusrser/"+str, true); 

,如果你已经在你的基本URL末尾添加'/'然后使用上述

;否则可以使用xhttp.open("GET", SiteUrl+"/lusrser/"+str, true);

希望这有助于..别人让我重新认识。

+0

感谢您的帮助 – user3266023

+0

欢迎........... :-) –