du997562 2015-03-27 10:59
浏览 46
已采纳

用于JSONP响应的Laravel JavaScript File响应

I'd like to generate a JavaScript file for JSONP data exchange. All fine, but I need / want to set the header as :

header("Content-Type: text/javascript");

or

header("Content-Type: application/javascript");

Is this possible in a response from a controller in Laravel 4, or do I need to create a view and set the header with PHP?

I'd like to output something like:

var obj = JSON.parse('{"item1":"value1","item2":"value2"}');

// then do whatever with the object
 $('#somediv').html(obj.item1);

Thanks for your help in advance

  • 写回答

2条回答 默认 最新

  • dongtanxi5676756 2015-03-27 12:41
    关注

    Okay, looks like I have to answer my question myself :-). Thanks to @terrylow for trying though.

    Here is the way to change the header of my response using my function in my controller

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

    variable content can also be filled with a view:

     $content = View::make('tools/jsonp_resonse'); // also possible with view
    

    Hope that will help someone...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致