Laravel Ajax 请求返回整个页面而不仅仅是数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20032328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Ajax request returning whole page rather than just data
提问by 001221
I'm trying to use ajax to find the next page on my pagination. However it keeps bringing in the whole body. I've taken a print screen to show the problem.
我正在尝试使用 ajax 在我的分页中查找下一页。然而,它不断带来整个身体。我已经采取了打印屏幕来显示问题。
I'm not an expert on ajax show would appreciate some help as to how I can rectify this issue?
我不是 ajax show 的专家,希望得到一些关于如何解决这个问题的帮助?
My code is below:
我的代码如下:
public function viewall()
{
$data["projects"] = $projects = Auth::user()->projects()->paginate(3);
if(Request::ajax())
{
$html = View::make('projects.viewall', $data)->render();
return Response::json(array('html' => $html));
}
return View::make('projects.viewall')->with('projects', $projects);
}
Js/js.js
js/js.js
$(".pagination a").click(function()
{
var myurl = $(this).attr('href');
$.ajax(
{
url: myurl,
type: "get",
datatype: "html",
beforeSend: function()
{
$('#ajax-loading').show();
}
})
.done(function(data)
{
$('#ajax-loading').hide();
$("#projects").empty().html(data.html);
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('No response from server');
});
return false;
});
viewall.blade.php
viewall.blade.php
@extends("layout")
@section("content")
<div class="container">
<h4>Your Projects</h4>
<div id="ajax-loading" class="alert alert-warning" style="display: none;">
<strong>Loading...</strong>
</div>
@if (Auth::check())
@if (count($projects) > 0)
@foreach ($projects as $project)
<div class="one-third column" id="projects">
{{ $project->project_name }}
{{ $project->project_brief }}
{{ date("d-m-Y", strtotime($project->start_day)) }}
</div>
@endforeach
@else
<h5 class="errorslist">You have no projects click <a class="errorslist" href="/project/create">here to create a project</a></h5>
@endif
@endif
<div class="sixteen columns">
{{ $projects->links() }}
</div>
</div>
@stop
采纳答案by egig
This line:
这一行:
$("#projects").empty().html(data.html);
will fill the #project
up with your returned html
which you created here:
将填写您在此处创建#project
的退货html
:
$html = View::make('projects.viewall', $data)->render()
;
$html = View::make('projects.viewall', $data)->render()
;
So,you just need to change projects.viewall
with only 'partial view' that you want to load.
因此,您只需要更改projects.viewall
要加载的“部分视图”即可。
Probably you don't need to extends your main layout.
可能你不需要扩展你的主布局。
回答by Marios Fakiolas
But you render a view here:
但是你在这里渲染一个视图:
$html = View::make('projects.viewall', $data)->render();
so html is created by Laravel and then you insert it as html into the #projects domElement.
所以 html 是由 Laravel 创建的,然后你将它作为 html 插入到 #projects domElement 中。
回答by dBlind
I experienced this kind of problem, just run
我遇到了这种问题,就运行
dd( json_decode( json_encode( Products::paginate(5) ), true) );
and can get a hint :)
并且可以得到提示:)
I have read a solution about this here:
我在这里阅读了有关此问题的解决方案: