0

我正在尝试进行动态搜索。当用户在输入字段中输入字符时,模型会返回结果。我没有使用此代码返回结果:

//here is my HTLML:
<input type='text' placeholder='Search By Email' class='searchterm' > 

//here is my jQuery:
$(".searchterm").keyup(function(e){
  var q = $(".searchterm").val();
  $.getJSON("searchpeeps",
  {
    q: q,
  },
  function(data) {
    $("ul.peepList").empty();
    $("ul.peepList").append(" Results for <b>" + q + "</b>");
    $.each(data.query.search, function(i,item){
      $("ul.peepList").append("<li>"++"</li>");
    });
  });
 });


//here is my get route:
Route::get('searchpeeps/{q}', array('as' => 'searchpeeps', 'uses' => 'PeopleController@searchpeeps'));


//here is my controller:
    public function searchpeeps($id)
    {

        $uid = Auth::user()->id;
        //$q = Input::get('q');
        return Invite::where('email', $id)
        ->where('user_id', $uid)    
        ->orderBy("created_at", "desc")
        ->groupBy('email')
        ->get(); 
    }
4

1 回答 1

0

您没有注册正确的 URL。解决此问题的两种方法并编写了一些代码以便更好地理解(根据您的需要进行调整)

1.调整PHP

// Here, modified URL
Route::get('searchpeeps', function(){
    return Input::get('q');
});

使用这个 Javascript

$(".searchterm").keyup(function(e){
    $.getJSON(
        "searchpeeps",
        { q: this.value },
        function(data) {
            console.log(data);
        }
    );
});

2.调整Javascript

$(".searchterm").keyup(function(e){
    $.getJSON(
        "searchpeeps/" + this.value,
        null,
        function(data) {
            console.log(data);
        }
    );
});

... 用这个 PHP

Route::get('searchpeeps/{q}', function($q){
    return $q;
});
于 2013-07-21T06:42:36.260 回答