Javascript & https - 获取相对路径的 XMLHttpRequest 对象 - 协议/端口是“继承的”吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4888306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:14:39  来源:igfitidea点击:

Javascript & https - XMLHttpRequest object to GET relative path - is protocol/port 'inherited'?

javascripthttpsxmlhttprequest

提问by mr.dancrane

If I use relative paths in Javascript to GET a page from a server (to display output inside a div), does Javascript use the same protocol/port as the page in which it was loaded?

如果我在 Javascript 中使用相对路径从服务器获取页面(以在 div 内显示输出),Javascript 是否使用与加载页面相同的协议/端口?

For example:

例如:

parent page is requested https://www.foo.com/bar.php

请求父页面https://www.foo.com/bar.php

JS code on bar.php:

bar.php 上的 JS 代码:

var turl = "/new_dir/index.php?r="+r;
if(window.XMLHttpRequest){  
    xmlhttp=new XMLHttpRequest();
}else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",turl,false);
xmlhttp.send(null);

Since the parent page was requested and served using https on port 443 does this mean that JS will send the GET request to the new page using the same protocol and port? Or will it send the request via http on port 80 since I did not specify a connection protocol in the 'turl' variable?

由于父页面是在端口 443 上使用 https 请求和服务的,这是否意味着 JS 将使用相同的协议和端口将 GET 请求发送到新页面?或者它会通过端口 80 上的 http 发送请求,因为我没有在 'turl' 变量中指定连接协议?

采纳答案by T.J. Crowder

It will use the same port and protocol, since you haven't specified anything else. Your URL is a relative referencein RFC-speak, details in Section 4.2 of the RFC. (I only happen to know the RFC section reference because I just recently found out about this nifty trickfor http/https stuff.)

它将使用相同的端口和协议,因为您没有指定任何其他内容。您的 URL 是RFC 中的相对引用,详细信息请参见RFC 的第 4.2 节。(我只是碰巧知道 RFC 部分参考,因为我最近才发现有关http/https 内容的这个漂亮技巧。)

So your request for

所以你的要求

/new_dir/index.php?r=blah

relative to the document

相对于文件

http://www.foo.com/bar.php

resolves to

决心

http://www.foo.com/new_dir/index.php?r=blah