Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

javascript - How can I return a view from an AJAX call in Laravel 5?

I'm trying to get an html table to return on an ajax call.

route:

Route::post('job/userjobs', 'JobController@userjobs');  

ajax on calling page:

function getUserJobs(userid) {
    $_token = "{{ csrf_token() }}";
    var userid = userid;
    $.ajax({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
        url: "{{ url('/job/userjobs') }}",
        type: 'POST',
        cache: false,
        data: { 'userid': userid, '_token': $_token }, //see the $_token
        datatype: 'html',
        beforeSend: function() {
            //something before send
        },
        success: function(data) {
            console.log('success');
            console.log(data);
            //success
            //var data = $.parseJSON(data);
            if(data.success == true) {
              //user_jobs div defined on page
              $('#user_jobs').html(data.html);
            } else {
              $('#user_jobs').html(data.html + '{{ $user->username }}');
            }
        },
        error: function(xhr,textStatus,thrownError) {
            alert(xhr + "
" + textStatus + "
" + thrownError);
        }
    });
}



//on page load
getUserJobs("{{ $user->id }}");

controller:

public function userjobs() {
    $input = Request::all();
    if(Request::isMethod('post') && Request::ajax()) {
        if($input['userid']) {
            $userjobs = Userjob::select('select * from user_jobs where user_id = ?', array($input['userid']));
            if(! $userjobs) {
                return response()->json( array('success' => false, 'html'=>'No Jobs assigned to ') );
            }
            $returnHTML = view('job.userjobs')->with('userjobs', $userjobs);
            return response()->json( array('success' => true, 'html'=>$returnHTML) );

        }
    }   
}

view:

@section('content')
<table class="table table-striped">
    <tbody>
@foreach ($userjobs as $userjob)
        <tr>
            <td><strong>{{ $userjob->title }}</strong><br />
            {{ $userjob->description }}
            </td>
        </tr>
@endforeach
</table>
@stop

Im not getting anything in the json.html data. nothing. If in the controller I say:

return response()->json( array('success' => true, 'html'=>'<span>html here</html>') );

This works just fine.

How can I return a view from an ajax call in Laravel 5.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The view() function just creates an instance of the View class. Not just an HTML string. For that you should call render():

$returnHTML = view('job.userjobs')->with('userjobs', $userjobs)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...