网址中带有和不带有 www 的 Access

编程入门 行业动态 更新时间:2024-10-22 16:21:30
本文介绍了网址中带有和不带有 www 的 Access-Control-Allow-Origin 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我制作了一个小的gwt应用程序并发布了它,但今天我发现了一个严重的问题.我知道同源策略问题,所以我将我的 gwt 应用程序和 rest json 应用程序放在同一台服务器上.但显然浏览器不考虑 http://www.xyz 和 http://xyz 作为相同的来源,因此当用户登陆 www.xyz 时,他无法从 http://xyz.

I've made a small gwt app and released it, but today I found a serious problem. I was aware of the same origin policy issue so I've put my gwt app and rest json app on the same server. But apparently browsers doesn't regard http://www.xyz and http://xyz as the same source so when a user lands on a www.xyz he can't get data from http://xyz.

这是消息:

XMLHttpRequest cannot load http://xyz/backend/... 
Origin http://www.xyz is not allowed by Access-Control-Allow-Origin.

处理这个问题的最佳方法是什么?我用谷歌搜索并首先找到了不适用于 tomcat 的 .htaccess 解决方案.我最终使用了一个空的登陆页面 index.html,只重定向到没有 www 的 url.这不是最好的解决方案,因为有人仍然可以输入带有 www 的 url,这不会索引页面,因此它不会被重定向.

What is the best way to deal with this? I've googled and first found .htaccess solution which doesn't work for tomcat. I ended up using a empty landing page index.html with only redirect to url without www in it. It's not the best solution because someone can still type in url with www which is not going to index page so it wont get redirected.

任何帮助将不胜感激.

推荐答案

除非绝对必要,否则不应在应用中使用绝对 URL.

You shouldn't use absolute URLs in your app unless absolutely necessary.

即如果应用程序可以从 http://www.example.

I.e. you should have "http://example" in your code if the app can be loaded from http://www.example.

例如,如果您想从中加载一些数据,例如http://example/abc/def,然后将 "/abc/def" 放入你的代码,不是 "http://example/abc/def".这样,浏览器会将 URL 解析为 http://www.example/abc/def,如果应用已从 http://www.example 或 http://example/abc/def 如果它是从 http://example 加载的.而且您永远不会冒险违反同源政策.

For instance, if you want to load some data from, e.g. http://example/abc/def, then put "/abc/def" in your code, not "http://example/abc/def". That way, the browser will resolve the URL to either http://www.example/abc/def if the app has been loaded from http://www.example, or to http://example/abc/def if it's been loaded from http://example. And you never risk to hit the Same-Origin Policy.

这篇关于网址中带有和不带有 www 的 Access-Control-Allow-Origin 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 04:27:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   www   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!