2016-11-14 75 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

歡迎........... :-) –