Javascript & https - XMLHttpRequest object for relative GET path - is the protocol / port "inherited"?

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

For instance:

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

JS code on bar.php:

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 a GET request to a new page using the same protocol and port? Or will he send the request via http to port 80, since I did not specify the connection protocol in the turl variable?

+4
source share
1 answer

It will use the same port and protocol since you did not specify anything. Your URL is a relative reference in RFC-talk, more in Section 4.2 RFC . (I only know the link to the RFC section because I recently found out about this great trick for http / https.)

So your request for

 /new_dir/index.php?r=blah 

regarding document

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

permits

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

Source: https://habr.com/ru/post/1338276/


All Articles