使用XMLHttpRequest进行Https请求 [英] Https request with XMLHttpRequest

查看:1908
本文介绍了使用XMLHttpRequest进行Https请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在问如何使用像https:// www这样的URL来请求请求。 gmail.com
我正在尝试这样的事情,但状态码是0

  var http = new XMLHttpRequest( ); 
var url =https://www.gmail.com;

http.open(GET,url);
http.onreadystatechange = function(){//当状态改变时调用一个函数。
if(http.readyState == 4&& http.status == 200){
//alert (http.responseText);
print(ok)
} else {
print(can not connect)
print(code:+ http.status)
print(http .responseText)
}
}
http.send(null);

我始终得到无法连接代码0并且没有任何回应



有什么想法?

解决方案

这会失败,原因有两个:



1)网址https://www.gmail.com实际上会尝试将您重定向到https://mail.google.com/mail/,然后依次会尝试将您重定向到登录页面。此重定向不会被列为错误

2)但更重要的是,您不能将XMLHttpRequests创建到不同的域,除非该域支持CORS(http:// www .html5rocks.com / EN /教程/ CORS /)。 GMail不支持CORS,所以这个请求将不起作用。


Im able to do Http requests (POST/GET) with XMLHttpRequest.

I'm asking how to do requests with URLs like "https://www.gmail.com" I was trying something like this but the status code is 0

var http = new XMLHttpRequest();
    var url = "https://www.gmail.com";

    http.open("GET", url);
    http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                    //alert(http.responseText);
                print("ok")
            }else{
                print("cannot connect")
                print("code:" + http.status)
                print(http.responseText)
            }
    }
    http.send(null);

I get always "cannot connect" "code" 0 and nothing as response

Any idea?

解决方案

This is going to fail for two reasons:

1) The url "https://www.gmail.com" actually tries to redirect you to "https://mail.google.com/mail/", which in turn will try to redirect you to a login page. This redirect is not being listed as an error

2) However more importantly, you cannot make XMLHttpRequests to a different domain, unless that domain supports CORS (http://www.html5rocks.com/en/tutorials/cors/). GMail does not support CORS, so this request will not work.

这篇关于使用XMLHttpRequest进行Https请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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