Javascript 控制器返回纯json数据,而不是停留在页面上

Javascript 控制器返回纯json数据,而不是停留在页面上,javascript,jquery,ajax,json,laravel,Javascript,Jquery,Ajax,Json,Laravel,我正在尝试向我们发送一个ajax请求以返回搜索结果。当我提交搜索时,它会在一个普通的黑白页面上返回我的json数据。我如何让它停留在页面上并表现出我的javascript的其余部分?我认为问题在于event.preventDefault无法正常工作,或者控制器中的return语句正在阻止任何进一步的事情发生 以下是我的代码片段: HTML 控制器 我的路线呢 你应该稍微改变一下你的javascript代码,像下面这样使用它 /** consider the hash before the

我正在尝试向我们发送一个ajax请求以返回搜索结果。当我提交搜索时,它会在一个普通的黑白页面上返回我的json数据。我如何让它停留在页面上并表现出我的javascript的其余部分?我认为问题在于event.preventDefault无法正常工作,或者控制器中的return语句正在阻止任何进一步的事情发生

以下是我的代码片段:

HTML

控制器

我的路线呢


你应该稍微改变一下你的javascript代码,像下面这样使用它

    /** consider the hash before the id of form you haven't added one so its not considering the form
also you need to change the url of you $.ajax call put url:'/portal/contacts' because jquery don't understand name of laravel route
**/
$('#contactsearchform').submit(function(e){
   ........
});

选择器缺少-$'contactsearchform'。提交。。。。如果没有这一点,它将被认为是一种搜索。啊,我讨厌当它是简单的事情,你看了一遍!这是我需要另一个开发人员在我的公司的原因之一。
$('contactsearchform').submit(function(event) {
    event.preventDefault();
    var dataString = $(this).serialize();
    console.log('Submitted Data:\n' + dataString);
    $.ajax({
        type: "POST",
        url: "/contacts.results",
        data: dataString,
        error: function(){
            console.log("Something is wrong. Please inform Admin");
        },
        success: function(resultData){
            console.log('Result Data:\n' + resultData);
            resultDataP = $.parseJSON(resultData);
            console.log('Parsed Data:\n' + resultDataP);
            if(resultDataP.table){
                $('#search_results').html(resultDataP.table);
                $('#search_results').fadeIn();
                $('#contactsearchform input').blur();
            }else{
                console.log("Something didn't work.");
            }
        }
    });
    return false;
});
public function showSearch()
{
    return View::make('portal.search');
}

public function doSearch()
{
    // Search for contacts here
    $search = Input::get('contactsearch');

    $contacts = DB::table('contacts')->where('last_name', 'LIKE', "$search")->get();

    if (count($contacts) != 0) {
        $response = [
            'status' => 'success',
            'msg' => 'Contacts matched your search.',
            'results' => $contacts
        ];
    }
    else {
        $response = [
            'status' => 'error',
            'msg' => 'No contacts matched your search.'
        ];
    }

    return Response::json($response);
}
// Search for contacts
Route::get( '/portal/contacts', array(
    'as' => 'contacts.search',
    'uses' => 'PortalController@showSearch'
) );

Route::post( '/portal/contacts', array(
    'as' => 'contacts.results',
    'uses' => 'PortalController@doSearch'
) );
    /** consider the hash before the id of form you haven't added one so its not considering the form
also you need to change the url of you $.ajax call put url:'/portal/contacts' because jquery don't understand name of laravel route
**/
$('#contactsearchform').submit(function(e){
   ........
});