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

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

图片转代码服务由CSDN问答提供 功能建议

我正在使用Symfony2和React构建应用程序。 目前我正在尝试通过AJAX调用将数据提取到反应前端。 无论我后来决定什么,我都需要知道如何配置路由或其他任何东西,以便能够进行跨域AJAX调用,即使是出于开发目的。 我尝试在config.yml中更改truste_host,在路由中添加主机参数但似乎没有任何效果。 如何在没有任何开销捆绑的情况下简单地使跨域工作?

我可以通过浏览器访问192.168.11.128:8000虚拟机上的Symfony2应用程序,以及来自 http://127.0.0.1 ,我无法得到答案,我真的需要这些域名不同。

谢谢 \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题