symfony2上的AJAX跨域

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

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

谢谢</ p> \ n </ div>

展开原文

原文

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个回答

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

dpbyr64224
dpbyr64224 你是男人! 谢谢!
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐