简体   繁体   English

symfony2上的AJAX跨域

[英]AJAX Cross-domain on symfony2

I am building an app with Symfony2 and React. 我正在使用Symfony2和React构建一个应用程序。 Currently I am trying to fetch data to the react frontend via AJAX calls. 目前我正在尝试通过AJAX调用将数据提取到反应前端。 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. 无论我后来决定什么,我都需要知道如何配置路由或其他任何东西,以便能够进行跨域AJAX调用,即使是出于开发目的。 I tried changing the truste_host in the config.yml, adding host parameter in routing but nothing seems to work. 我尝试在config.yml中更改truste_host,在路由中添加主机参数但似乎没有任何效果。 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. 我有192.168.11.128:8000虚拟机上的Symfony2应用程序可以通过浏览器和来自http://127.0.0.1的AJAX调用访问,我无法得到答案,我真的需要这些域名不同。

Thanks 谢谢

The Response of the server need to have the header Access-Control-Allow-Origin ( More info here ). 服务器的响应需要具有标头Access-Control-Allow-Origin此处有更多信息 )。 As Example of a Symfony2 Controller Response with the value * (allowing any origin to access the resource.), you can build a Response as follow: 作为具有值*(允许任何源访问资源)的Symfony2 Controller响应的示例,您可以构建响应,如下所示:

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

Hope this help 希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM