Skip to content
Advertisement

JSONP with Laravel

Browser sends JSONP requets to Laravel, Laravel returns the results to browser. In the brower console, I get the warning:

Resource interpreted as Script but transferred with MIME type text/html:

I believe this is due to improper headers in the JSONP response? How should this warning be fixed?

PHP

$callback = Input::get('callback');
$result = DB::table('users')->find(123);

return $callback . '(' . json_encode($result) . ')';

Response::JSON($result) will return the usual JSON response I believe, not the JSONP variant with the callback function name wrapping around the payload.

Advertisement

Answer

You can provide the headers in the Response::json() call.

Response::json($result, 200, array('Content-Type' => 'application/javascript'));

The correct MIME type for JSONP is application/javascript.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement