javascript res.send 和 res.render 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30847070/
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
res.send and res.render calls
提问by Raj
I am trying to determine if i can call res.send(data) and then res.render('reports') simultaneously.
我想确定是否可以同时调用 res.send(data) 和 res.render('reports') 。
To explain further in detail, when i route to '/reports', first on my server side i making a REST call to an API which returns back json data. Now i want this json data to be accessed on the client, for which i am making an ajax call from my javascript. Hence the use of res.send(), but i also want to render the page in this call
为了进一步详细解释,当我路由到“/reports”时,首先在我的服务器端,我对返回 json 数据的 API 进行 REST 调用。现在我希望在客户端上访问此 json 数据,为此我正在从我的 javascript 进行 ajax 调用。因此使用 res.send(),但我也想在此调用中呈现页面
So it looks like the following on my server side code
所以它在我的服务器端代码中看起来如下
router.get('/reports', function(req,res){
//Making the REST Call to get the json data
//then
res.send(json);
res.render('reports');
});
Every time i hit the '/reports' on the browser, I see the json value instead of the page being rendered and my console throws an Error: Can't set headers after they are sent.
每次我在浏览器上点击“/reports”时,我都会看到 json 值而不是正在呈现的页面,并且我的控制台会抛出一个错误:发送后无法设置标头。
回答by robertklep
You could use content negotiationfor that, where your AJAX request sets the Accept
header to tell your Express server to return JSON instead of HTML:
您可以为此使用内容协商,其中您的 AJAX 请求设置Accept
标头以告诉您的 Express 服务器返回 JSON 而不是 HTML:
router.get('/reports', function(req,res) {
...
if (req.accepts('json')) {
return res.send(theData);
} else {
return res.render('reports', ...);
};
});
Alternatively, you can check if the request was made with an AJAX call using req.xhr
(although that's not 100% failsafe).
或者,您可以检查请求是否是通过 AJAX 调用发出的req.xhr
(尽管这不是 100% 的故障安全)。
回答by cs04iz1
No you can't do both, but you could render the page and send the data at the same time:
不,您不能同时执行这两项操作,但您可以渲染页面并同时发送数据:
res.render('reports',{data:json});
and then access those data in the newly rendered page.
然后在新呈现的页面中访问这些数据。
alternatively you could send a flag when making the call , and then decide whether you want to render or send based on this flag.
或者,您可以在拨打电话时发送一个标志,然后根据此标志决定是要呈现还是发送。
回答by Swaraj Giri
Ideally, it needs to be 2 separate route, one spitting json and other rendering a view. Else, you could pass a url param, depending on which you return json or render a view.
理想情况下,它需要是 2 条独立的路线,一条是 json,另一条是渲染视图。否则,您可以传递一个 url 参数,具体取决于您返回 json 或呈现视图。
router.get('/reports/json', function(req,res){
var data = JSON_OBJECT;
res.send(data);
});
router.get('/reports', function(req,res){
var data = JSON_OBJECT;
res.render('path-to-view-file', data);
});
回答by Quentin
No, you can't. You can only have a single response to a given request. The browser is either expecting an HTML document or it is expecting JSON, it doesn't make sense to give it both at once.
不,你不能。对于给定的请求,您只能有一个响应。浏览器要么需要 HTML 文档,要么需要 JSON,同时提供两者是没有意义的。
render
just renders a view and then calls send
.
render
只呈现一个视图,然后调用send
.
You could write your view to output an HTML document with a <script>
element containing your JSON in the form of a JavaScript literal.
您可以编写视图以输出一个 HTML 文档,其中<script>
包含一个包含 JavaScript 文本形式的 JSON的元素。