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

Controller returns plain json data instead of staying on page

本文关键字:停留在 返回 json 数据 控制器      更新时间:2023-09-26

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

以下是我的代码片段:

HTML

<div id="contactform">
    <p>Search for a Contact</p>
    {{ Form::open(array('route' => 'contacts.results', 'method' => 'post', 'id' => 'contactsearchform')) }}
        {{ Form::text('contactsearch', null, array('id' => 'contactsearch', 'placeholder' => 'Name')) }}
        <button type="submit">Search</button>
    {{ Form::close() }}
</div>
<div id="search_results">
    Search Results
    <table cellpadding="0" cellspacing="0" border="0">
        <thead>
            <tr>
                <td class="name first">Name</td>
                <td class="phone">Phone</td>
                <td class="email">Email</td>
                <td class="lot">Lot</td>
                <td class="edit last"></td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="name first"><a href="#" class="contact"></a></td>
                <td class="phone"></td>
                <td class="email"></td>
                <td class="edit last"></td>
            </tr>
        </tbody>
    </table>
</div>

Javascript

$('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'
) );

您应该稍微更改一下您的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){
   ........
});