dongshan9619 2016-05-29 17:29
浏览 199
已采纳

symfony2上的AJAX跨域

I am building an app with Symfony2 and React. Currently I am trying to fetch data to the react frontend via AJAX calls. No matter what I decide later, I need to know how can I configure the routing or whatever in order to be able to make cross domain AJAX calls, even for development purposes. I tried changing the truste_host in the config.yml, adding host parameter in routing but nothing seems to work. How can I get the cross-domain working, simply, without any overhead bundles?

I have the Symfony2 app on 192.168.11.128:8000 virtual machine accessible via browser and an AJAX call from http://127.0.0.1, there is no way I can get the answer, and I really need these domains to be different.

Thanks

  • 写回答

1条回答 默认 最新

  • duanmuybrtg1231 2016-05-29 18:04
    关注

    The Response of the server need to have the header Access-Control-Allow-Origin (More info here). As Example of a Symfony2 Controller Response with the value * (allowing any origin to access the resource.), you can build a Response as follow:

        return new JsonResponse($data, 200, array('Access-Control-Allow-Origin'=> '*'));
    

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料