服务器端-第一次加载时出现bootsrap typeahead内部错误-未找到laravel http错误


server side - bootsrap typeahead internal error on first load - with laravel http error not found

我正在尝试在表单中使用twitter引导程序typeahead,并使用服务器端查询从数据库中获取记录。我已经启动并运行了该功能,一旦我开始在3个字符后输入搜索,它就会获取数据,这很好。然而,在第一次加载页面时,我从主引导程序typeahead.js脚本中得到了一个500错误的异常,即:

NotFoundHttpException

我在使用页面时不会出现错误——只是在第一次加载时。

因此,我假设脚本对服务器端的查询进行了初始调用,其中可能有一个空查询,这可能是原因。我试过在laravel上的路由和搜索功能中设置默认值,但仍然存在错误。

页面按原样运行,但我不希望出现此错误。我从哪里开始看?我甚至不确定脚本提出了什么请求来抛出错误。。。

这是脚本-初始化typeahed函数:

$('#typeahead').typeahead({
source: function(query, process) {
    if(query===null) {
        query = 'aaa';
    }
    $.ajax({
        url: '/customers/typeahead/'+query,
        type: 'GET',
        dataType: 'json',
        //data: "query=" + query,
        async: false,
        success: function(data) {
            customers = [];
            map = {};
            $.each(data, function(i, customer) {
                //map object with unique name for each returned customer
                map[customer.first_name + ' ' + customer.last_name + ': ' + customer.email + ' - ' + customer.id ] = customer;
                customers.push(customer.first_name + ' ' + customer.last_name + ': ' + customer.email + ' - ' + customer.id);
            });
            process(customers);
        }
    });
},
minLength: 3,
updater: function(item) {
    alert("selected");
    id = map[item].id;
    fetchCustomer(id);
    return item;
}
});

这是laravel路线:

Route::get('customers/typeahead/{qry?}', function ($qry = 'a') {
    return Customer::customerTypeahead($qry);
});

以下是laravel模型函数:

static public function customerTypeahead($qry) {
    //$qry = Input::get('query');
    $qry='%'.$qry.'%';
    $customers=Customer::where('first_name','like',$qry)
        ->orWhere('last_name','like',$qry)
        ->orWhere('email','like',$qry)
        ->get(array('first_name','last_name','email','id'))->toJson();
    return $customers;
}

有什么想法可以引发最初的错误以及如何开始跟踪吗?

感谢

尝试将完整的网站url添加到jQuery的url对象中。如果你想要动态url,你可以添加

<script>var WEBSITE_URL = {{ URL::to('/') }};</script>

在js应用程序之前,然后

...
$.ajax({
    url: WEBSITE_URL + 'customers/typeahead/'+query,
...

试着使用firebug来查看发送了什么和返回了什么,这样你就可以更好地定位错误所在。