admin管理员组

文章数量:1287645

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?

For example:

parent page is requested .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 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?

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?

For example:

parent page is requested https://www.foo./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 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?

Share Improve this question asked Feb 3, 2011 at 15:49 mr.dancranemr.dancrane 711 gold badge1 silver badge3 bronze badges 1
  • It will inherit, try it out, you can sue fiddler to see where request are being sent or you can just use firefox with firebug and look in the nettab. – Martin Jespersen Commented Feb 3, 2011 at 15:53
Add a ment  | 

1 Answer 1

Reset to default 7

It will use the same port and protocol, since you haven't specified anything else. Your URL is a relative reference in 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 trick for http/https stuff.)

So your request for

/new_dir/index.php?r=blah

relative to the document

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

resolves to

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

本文标签: Javascript amp httpsXMLHttpRequest object to GET relative pathis protocolport 39inherited39Stack Overflow