我想为JSONP数据交换生成一个JavaScript文件。一切都很好,但我需要/想将标题设置为:
header("Content-Type: text/javascript");
或
header("Content-Type: application/javascript");
这在Laravel 4中的控制器的响应中可能吗?或者我需要创建一个视图并用PHP设置头部吗?
我想输出这样的东西:
var obj = JSON.parse('{"item1":"value1","item2":"value2"}');
// then do whatever with the object
$('#somediv').html(obj.item1);
感谢您提前提供的帮助
好吧,看起来我必须自己回答我的问题:-)。感谢@terrylow的尝试。
以下是使用我在控制器中的功能更改响应标头的方法
public function javascriptResponse(){
$statusCode = 200;
$content = "var obj = JSON.parse('{'"item1'":'"value1'",'"item2'":'"value2'",'"some'":'"whoaevaNew'"}');";
$response = Response::make($content, $statusCode);
$response->header('Content-Type', 'application/javascript');
return $response;
}
可变内容也可以填充一个视图:
$content = View::make('tools/jsonp_resonse'); // also possible with view
希望这能帮助到别人。。。
您可以使用laravel 提供的此方法
return Response::jsonp($callback, $data,$status, $header);