[LayaAir 2.0]部署web后无法请求访问HTTP API接口数据,调试模式下可以访问获取

Access to XMLHttpRequest at 'http://192.168.3.70:8081/api/test?data=test' from origin 'http://192.168.3.70:8082' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Sylar

赞同来自:

     自己回复吧,我是这么解决的,在服务的API接口添加以下代码就可以了
        //初始化
                _config = new HttpSelfHostConfiguration(url);

                //API路由
                _config.Routes.MapHttpRoute(
                    "API Default",
                    "api/{controller}/{username}",
                    new { username = RouteParameter.Optional });

                //这是重点,从配置文件的appsettings节点中读取跨域的地址
                var cors = new EnableCorsAttribute("*", "*", "*");
                _config.EnableCors(cors);

熊猫大侠

赞同来自:

跨域问题

Laya_XS

赞同来自:

你这是本地的域名地址包括端口,请求了另一个域名的地址的资源或数据。这个情况下是存在跨域问题的。解决跨域问题的方案就是在访问端增加上允许跨域的限制即可。

该问题目前已经被锁定, 无法添加新回复

商务合作
商务合作