Laravel 4 如何使用ajax 和json 返回多个视图数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20174608/
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 4 how to return multiple view data with ajax and json?
提问by user2002495
Basically I'm trying to make a popup using this ajax:
基本上我正在尝试使用这个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
path: msg.url
, "", msg.url
false
And I'm returning a view value and it's url with this:
我正在返回一个视图值,它是带有以下内容的 url:
$data = json_encode(array(
'view' => View::make('layouts.read'),
'url' => 'news/read/2013/11/24/test-title-seperate-with-dash'
));
return $data;
This all works really well except that I can't get the view value from laravel (it returns Object object
in javascript). But it returns nicely if I directly write it like return View::make('layouts.read')
. Why is that?
这一切都非常有效,只是我无法从 laravel 获取视图值(它Object object
在 javascript 中返回)。但是如果我直接像return View::make('layouts.read')
. 这是为什么?
Additionally (don't have to answer, not primary question), the back button on my browser doesn't work when I use pushState
, is it a bug?
另外(不必回答,不是主要问题),当我使用时浏览器上的后退按钮不起作用pushState
,这是一个错误吗?
回答by The Alpha
You may try this
你可以试试这个
$data = json_encode(array(
'view' => (String)View::make('layouts.read'),
'url' => 'news/read/2013/11/24/test-title-seperate-with-dash'
));
return $data;
Also, you can use
此外,您可以使用
View::make('layouts.read')->render();
View::make('layouts.read')->__toString();
Also, Laravel
provides Response::json()
method for same reason (instead of json_encode
).
此外,出于同样的原因Laravel
提供Response::json()
方法(而不是json_encode
)。