如何通过 Laravel 中的 ajax/jquery 将刀片或 php 内容加载到视图中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24812785/
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
How to load blade or php content into a view via ajax/jquery in laravel?
提问by Baker Johnson
As the title says, I am trying to dynamically load content in a view using ajax requests. I know this can be done if you are using html
elements e.g. ("#div_place").html(<p>...)
. The problem lies when I would like to load some php/blade objects into a div for instance. Is this possible or is there a different way to go about achieving the result I want.
正如标题所说,我正在尝试使用 ajax 请求在视图中动态加载内容。我知道如果您使用html
元素,例如("#div_place").html(<p>...)
. 问题在于当我想将一些 php/blade 对象加载到 div 中时。这是可能的还是有不同的方法来实现我想要的结果。
回答by Kryten
This is pretty straightforward. Assuming you're using jQuery...
这很简单。假设您正在使用 jQuery ...
create a route that will respond to the AJAX call and return an HTML fragment
Route::get('test', function(){ // this returns the contents of the rendered template to the client as a string return View::make("mytemplate") ->with("value", "something") ->render(); });
in your javascript:
$.get( "test", function (data) { $("#my-content-div").html(data); } );
创建将响应 AJAX 调用并返回 HTML 片段的路由
Route::get('test', function(){ // this returns the contents of the rendered template to the client as a string return View::make("mytemplate") ->with("value", "something") ->render(); });
在你的 javascript 中:
$.get( "test", function (data) { $("#my-content-div").html(data); } );
回答by Baker Johnson
After some digging some more I found this which helped me to solve the problem.
经过一些挖掘,我发现这有助于我解决问题。
You have to use the .load("url/page/...")
via jquery and then set a route in the routes.php file that displays a view with the data that needs to be loaded e.g.
您必须使用.load("url/page/...")
via jquery,然后在 routes.php 文件中设置一个路由,该文件显示一个包含需要加载的数据的视图,例如
Route::get('/url/page/...', function(){
return View::make('test');
});
This returns the necessary php code which needed to be loaded dynamically, cheers.
这将返回需要动态加载的必要 php 代码,欢呼。