Php Laravel 4如何使用ajax和json返回多视图数据?
基本上,我尝试使用以下ajax创建一个弹出窗口:Php Laravel 4如何使用ajax和json返回多视图数据?,php,jquery,ajax,json,laravel,Php,Jquery,Ajax,Json,Laravel,基本上,我尝试使用以下ajax创建一个弹出窗口: $.ajax({ type: 'GET' url: 'news/read' dataType: 'json' cache: false timeout: 10000 }).done (msg) -> $("article#pop-read").empty().html msg.view processing = false window.history.pushState
$.ajax({
type: 'GET'
url: 'news/read'
dataType: 'json'
cache: false
timeout: 10000
}).done (msg) ->
$("article#pop-read").empty().html msg.view
processing = false
window.history.pushState
path: msg.url
, "", msg.url
false
我返回一个视图值,它的url如下:
$data = json_encode(array(
'view' => View::make('layouts.read'),
'url' => 'news/read/2013/11/24/test-title-seperate-with-dash'
));
return $data;
这一切都非常有效,除了我无法从laravel获取视图值(它在javascript中返回Object-Object
)。但是如果我直接像returnview::make('layouts.read')
那样编写它,它会返回很好的结果。为什么呢
另外(不必回答,不是主要问题),当我使用pushState
时,浏览器上的后退按钮不起作用,这是一个错误吗?您可以试试这个
$data = json_encode(array(
'view' => (String)View::make('layouts.read'),
'url' => 'news/read/2013/11/24/test-title-seperate-with-dash'
));
return $data;
此外,您还可以使用
View::make('layouts.read')->render();
View::make('layouts.read')->__toString();
另外,
Laravel
出于同样的原因提供了Response::json()
方法(而不是json\u encode
)。好的,谢谢,所有解决方案都有效。现在转到pushState
问题。知道如何将视图解析为json响应吗?如何使用$('#element').html(data.html)为我的页面中的元素提供视图的html?发布一个问题,给我链接,我会回答,在问题中详细说明。