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

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题