jQuery Laravel:使用 AJAX 请求渲染部分视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24984073/
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: rendering partial views with AJAX requests
提问by dabadaba
I am trying to make a single page CRUD application and I am using AJAX with jQuery. In this case, I submit the form and store a new country in my database asynchronously and then render a partial view with the new data.
我正在尝试制作一个单页 CRUD 应用程序,并且我正在将 AJAX 与 jQuery 一起使用。在这种情况下,我提交表单并在我的数据库中异步存储一个新国家,然后使用新数据呈现部分视图。
This is my script and the method that retrieves the countries from the database and returns the partial view.
这是我的脚本和从数据库中检索国家并返回局部视图的方法。
$('#create-country-form').submit(function(event) {
$.post('/country/store', $('#create-country-form').serialize(), function() {
$.get('/country/all', function(data) {
$('#countries-table').empty();
$('#countries-table').append(data['html']);
});
});
event.preventDefault();
});
class CountryController extends BaseController {
public function all() {
$countries = Country::All();
$html = View::make('countries.list', compact('countries'))->render();
return Response::json(['html' => $html]);
}
// ...
}
However, I don't like the idea of actually rendering the view in the page using jQuery, it feels like this should be Laravel's work.
但是,我不喜欢使用 jQuery 在页面中实际渲染视图的想法,感觉这应该是 Laravel 的工作。
How can I render with Laravel a partial view along with the use of AJAX, and not having to delegate that work to jQuery (append()
and empty()
)?
如何使用 Laravel 渲染局部视图以及 AJAX,而不必将该工作委托给 jQuery(append()
和empty()
)?
采纳答案by Laurence
You are getting confused.
你越来越糊涂了。
Laravel is alreadydoing the rendering of the partial. You are calling View::make()
and returning the renderedview to $html
.
Laravel已经在做局部的渲染了。您正在调用渲染视图View::make()
并将其返回到.$html
Then you are passing the rendered view to jQuery, which is simply displaying the data given to it - its not doing any of the calculations itself.
然后你将渲染的视图传递给 jQuery,它只是显示提供给它的数据 - 它本身不进行任何计算。
回答by Mubashar
Pass your view to view helper, render it into a variable and return that variable as response to AjAx.
将您的视图传递给视图助手,将其呈现为一个变量并将该变量作为对 AjAx 的响应返回。
$view=view('your-view-blade');
$view=$view->render();
$responseData->responseSuccess($view);
In callback of your AjAx you can use that rendered HTML, you are free to append that HTML to any element.
在 AjAx 的回调中,您可以使用呈现的 HTML,您可以随意将该 HTML 附加到任何元素。
回答by Imtiaz Pabel
Use string method before view file
在查看文件之前使用字符串方法
return (String) view('Company.allUserAjax');
回答by Mohammad Nizar Arbash
you can render the view by render()
function, then return it by JSON response
您可以通过render()
函数呈现视图,然后通过 JSON 响应返回它
$arr['key1'] = value;
$arr['key2'] = value;
$view = view('view.name', $arr)->render();
return Response::json(['status' => 200, 'view' => $view]);