symfony2上的AJAX跨域 [英] AJAX Cross-domain on symfony2

查看:161
本文介绍了symfony2上的AJAX跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Symfony2和React构建一个应用程序。目前,我正在尝试通过AJAX调用将数据获取到React前端。不管我以后决定什么,我都需要知道如何配置路由或进行任何配置,以便能够进行跨域AJAX调用,甚至出于开发目的。我尝试更改config.yml中的truste_host,在路由中添加host参数,但似乎无济于事。我如何在没有任何开销捆绑的情况下简单地使跨域工作?

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?

我在192.168.11.128:8000虚拟机上拥有Symfony2应用,可通过浏览器访问该文件,并从 http://127.0.0.1 ,我无法获得答案,我真的需要这些域有所不同。

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.

谢谢

推荐答案

服务器的响应需要标头 Access-Control-Allow-来源更多信息在这里)。
作为Symfony2控制器响应的示例,该响应的值为*(允许任何来源访问资源。),您可以按以下方式构建响应:

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'=> '*'));

希望获得帮助

这篇关于symfony2上的AJAX跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆