我是使用ajax的新手。我有一个ajax程序,当一个人在搜索栏中查找用户时,它会搜索用户。它在每个键上执行的操作是使用ajax从数据库中提取数据以供推荐以帮助用户选择。 但问题是它对大多数页面都有效,除了编辑和显示特定数据的页面。
这里是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','UserController@index');
route::get('/createuser','UserController@create');
route::post('/createuser','UserController@store');
route::get('/edituser/{id}','UserController@edit');
route::post('/edituser','UserController@update');
route::get('/showuser/{id}','UserController@show');
因为仅在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 :(得分:1)
如果您使用的是刀片文件,则可以使用此技巧。
像这样定义一个javascript变量。
var SiteUrl = '{{ url::to("") }}';
你可以用这样的ajax网址连接它。
xhttp.open("GET", SiteUrl+"lusrser/"+str, true);
如果您在baseURL中添加了'/'
,请使用上面的
否则您可以使用xhttp.open("GET", SiteUrl+"/lusrser/"+str, true);
希望这有帮助......别的让我知道。