如何通过Laravel中的Ajax/jquery将Blade或PHP内容加载到视图中? [英] How to load blade or php content into a view via ajax/jquery in laravel?
问题描述
正如标题所说,我正在尝试使用ajax请求在视图中动态加载内容.我知道如果您使用 html
元素,例如(#div_place").html(< p> ...)
.问题出在例如我想将一些php/blade对象加载到div中时.这是否可能,或者有其他方法可以达到我想要的结果.
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.
推荐答案
这非常简单.假设您正在使用jQuery ...
This is pretty straightforward. Assuming you're using jQuery...
-
创建一条将响应AJAX调用并返回HTML片段的路由
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();
});
在您的JavaScript中:
in your javascript:
$.get(
"test",
function (data) {
$("#my-content-div").html(data);
}
);
这篇关于如何通过Laravel中的Ajax/jquery将Blade或PHP内容加载到视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!