21

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.

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);
}

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-Type as expected, however when I try to redefine the Content-Type header it doesn't seem to do anything even with the replace parameter set as true.

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

Thanks a lot for your help

4

6 回答 6

47

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

$contents = View::make('embedded')->with('foo', $foo);
$response = Response::make($contents, $statusCode);
$response->header('Content-Type', 'application/javascript');
return $response;
于 2013-09-08T15:29:39.623 回答
12

In Laravel 5.4 you can do this:

$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.

于 2017-04-30T17:36:22.880 回答
8

In Laravel 5.6:

return response()
    ->view('embedded', ['foo' => $foo])
    ->header('Content-Type', 'application/javascript');
于 2018-07-21T05:47:51.757 回答
3

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:

return Response::json($json);

assuming, obviously, that $json contains your 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 = 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.)

于 2014-05-14T18:05:41.800 回答
3

Response::json() is not available anymore.

You can use response->json() instead.

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

Gives:

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

With the corresponding headers.

于 2015-06-04T07:43:59.867 回答
0

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

于 2018-01-22T20:37:38.460 回答