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

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

My controller:

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

视图本身:

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

我发现,在我的视图中,我可以使用header()来添加自定义标题,如预期的X-Content-Type,然而,当我try 重新定义Content-Type标题时,即使replace参数设置为true,它似乎也没有任何作用.

我肯定错过了一些显而易见的东西,如果你能给我指出,我将不胜感激:)

非常感谢您的帮助

推荐答案

Laravel允许您通过Response类修改标题信息,因此您必须使用它.从视图中删除header行,然后在控制器中这样try :

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

Laravel相关问答推荐

laravel错误更新使用外键的数据库

Livewire 3软件包开发

无法在 Laravel 项目中安装 @vitejs/plugin-vue

为什么 laravel 模型会重复一组数据以及如何(如果可能)只有一组数据?

如何在 Laravel 5.5 中扩展 vendor 包服务提供者

Laravel:`loadMissing` 函数的目的是什么?

会话中的 Laravel 存储数组

Laravel 干预图像 GD 库扩展

何时在 Laravel 中使用 Repository vs Service vs Trait?

Web 服务器如何处理请求?

Laravel 控制器构造

Laravel 同步错误

如何在 Laravel 中使用旧输入重定向?

用于集合的 Laravel 5.5 API 资源

调用未定义的方法 Illuminate\Foundation\Application::bindShared()

laravel:Eloquent的例子将数据插入数据库

Laravel 验证存在于不存在的地方

Eloquent - 带有字符串值而不是列标题的连接子句

Laravel 5 new auth:获取当前用户以及如何实现角色?

连接到 tcp://smtp.mail.yahoo.com:465 超时