Javascript 在同一主机url上阻止跨源请求
在我的javascript中,我有Javascript 在同一主机url上阻止跨源请求,javascript,Javascript,在我的javascript中,我有 client.base_url = "https://qp-t3.herokuapp.com"; var url = client.base_url + "/account/new?name=" + client.name; http.get({ url: url, onload: function() { account = JSON.parse(this.responseText);
client.base_url = "https://qp-t3.herokuapp.com";
var url = client.base_url + "/account/new?name=" + client.name;
http.get({
url: url,
onload: function() {
account = JSON.parse(this.responseText);
client.account = account;
client.findGame();
}
});
}
我得到了这个错误:
XMLHttpRequest cannot load https://qp-t3.herokuapp.com/account/new?name=rstrsrtsrt. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://qp-t3.herokuapp.com' is therefore not allowed access.
我知道那是因为我在发送http。我正在使用http.min.js
如何发送https
编辑:当我使用该基本url调用http.get时,http.min.js库将转换为http
我将尝试使用这个技巧
Mixed Content: The page at 'https://qp-t3.herokuapp.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://qp-t3.herokuapp.com/account/new?name=v'. This request has been blocked; the content must be served over HTTPS.
“我如何发送https?”不知道你的意思。您请求的是
https
,但您似乎在页面中加载了http
。如果您的服务器配置为https,则只需使用https打开页面。按照错误消息中的说明执行:发送允许从http://qp-t3.herokuapp.com
(如果您确实希望通过http加载主页,但希望通过https加载ajax).Acutally我认为这是一个误解-不是您发送http,而是源页面(执行http.get()
调用的上下文)是通过http访问的。因此,您可能希望强制页面用户访问https位置,或者使用一些代码来检测用户是使用http还是https,并使用适当的base\u url
@ChrisO'Kelly是正确的。我个人倾向于第一种选择,即首先强制页面加载https。但是,如果使用第二个选项,则可以使用base_url=“//qp-t3.herokuapp.com”,而不是编写代码代码>,将自动使用用户当前使用的协议。(即http->http或https->https)。@ChrisO'Kelly你是对的——我没有注意到基本url没有设置为https。现在可以在普通浏览器中加载!