php 如何让 Laravel 将视图的“Content-Type”标头作为“application/javascript”返回?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18685321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:01:07  来源:igfitidea点击:

How to make Laravel return a View's "Content-Type" header as "application/javascript"?

phplaravellaravel-4

提问by Qwindoo

I'm trying to output a dynamic javascript file for inclusion from external websites with the [script src=""]tag. As the view is using the Blade engine, it's rendered as text/html.

我正在尝试输出一个动态 javascript 文件,以便从带有[script src=""]标记的外部网站中包含。由于该视图使用 Blade 引擎,所以它呈现为text/html.

I'd like the Content-Typeheader to be set to application/javascriptfor this view only to avoid Chrome bugging me with messages like "Resource interpreted as Script but transferred with MIME type text/html:"

我希望将此视图的Content-Type标头设置application/javascript为仅用于避免 Chrome 用诸如“ Resource interpreted as Script but transferred with MIME type text/html:”之类的消息烦扰我

My controller:

我的控制器:

{
    // ...
    return View::make('embedded')->with('foo', $foo);
}

The view itself:

视图本身:

<?php
header('Content-Type: application/javascript; charset=UTF-8', true);
?>(function(jQuery) {
    // append stylesheets to <head>
    var file;
    // ...
})(jQuery);

I've found that I can use header()in my view to add custom headers like X-Content-Typeas expected, however when I try to redefine the Content-Typeheader it doesn't seem to do anything even with the replaceparameter set as true.

我发现我可以header()在我的视图中使用像X-Content-Type预期的那样添加自定义标题,但是当我尝试重新定义Content-Type标题时,即使replace参数设置为true.

I'm surely missing something obvious here, would appreciate your pointing it out to me :)

我肯定在这里遗漏了一些明显的东西,感谢您向我指出:)

Thanks a lot for your help

非常感谢你的帮助

回答by ciruvan

Laravel lets you modify header information via the Response class, so you have to make use of it. Remove the headerline from your view and try it like this in your controller:

Laravel 允许您通过 Response 类修改头信息,因此您必须使用它。header从您的视图中删除该行并在您的控制器中像这样尝试:

$contents = View::make('embedded')->with('foo', $foo);
$response = Response::make($contents, $statusCode);
$response->header('Content-Type', 'application/javascript');
return $response;

回答by Mosty Mostacho

In Laravel 5.4 you can do this:

在 Laravel 5.4 中你可以这样做:

$contents = view('embedded')->with('foo', $foo);
return response($contents)->header('Content-Type', 'application/javascript');

BTW, there is no need to set the header in the view.

顺便说一句,不需要在视图中设置标题。

回答by Finesse

In Laravel 5.6:

在 Laravel 5.6 中:

return response()
    ->view('embedded', ['foo' => $foo])
    ->header('Content-Type', 'application/javascript');

回答by iconoclast

If you just have JSON in a variable and you want to send it to the browser with the proper content-type set in the header, all you need to do is this:

如果您只有一个变量中的 JSON 并且您想将它发送到浏览器,并在标题中设置正确的内容类型,您需要做的就是:

return Response::json($json);

assuming, obviously, that $jsoncontains your JSON.

显然,假设其中$json包含您的 JSON。

Depending on the details of your situation it might make more sense to use views (rather than building your JSON by concatenating strings) but this is still an option if you use a view to build the string. Something approximately along these lines should work:

根据您的情况的详细信息,使用视图(而不是通过连接字符串来构建 JSON)可能更有意义,但如果您使用视图来构建字符串,这仍然是一个选项。大致沿着这些方向应该可以工作:

$json = View::make('some_view_template_that_makes_json') -> with ('some_variable', $some_variable)
return Response::json($json);

(Apologies if I missed some part of the question that requires a more manual approach! At least this should be useful to someone else coming here and wondering how to send JSON from Laravel with the right content-type set.)

(如果我错过了需要更多手动方法的问题的某些部分,请道歉!至少这对于来到这里并想知道如何使用正确的内容类型集从 Laravel 发送 JSON 的其他人来说应该是有用的。)

回答by Juangui Jordán

Response::json() is not available anymore.

Response::json() 不再可用。

You can use response->json() instead.

您可以改用 response->json() 。

use Illuminate\Contracts\Routing\ResponseFactory;
$foobar = ['foo' => 0, 'bar' => 'baz'];
return response()->json($foobar);

Gives:

给出:

{"foo":0,"bar":"baz"}

With the corresponding headers.

带有相应的标题。

回答by roll

This worked for me return Response::view($landing)->header('X-Frame-Options', 'DENY');

这对我有用 return Response::view($landing)->header('X-Frame-Options', 'DENY');