如果不能确定端口,如何用AJAX发送HTTP请求?

如果我用脚本发一个AJAX请求,比如说:

var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
x.onreadystatechange=function(){ //do something
};
x.open('GET','getSomeData.jsp?id=aaaa');
x.send(null);

如果用tomcat端口为8080的话,HTTP请求不能正常收到。
难道用:
x.open('GET','http://localhost:8080.getSomeData.jsp?id=aaaa');???

请问有经验的人是怎么做的。

不用重新做的,只要在
URL urlfile = new URL("http://" + ip + ":8080/somefolderName/" + fileName);

就可以了。

以下为本机测试代码:

public static void main(String[] args) throws IOException {

    URL url = new URL("http://localhost:8080/");

    URLConnection conn = url.openConnection();

    BufferedReader reader = new BufferedReader(new InputStreamReader(conn
            .getInputStream()));

    String line = null;

    while ((line = reader.readLine()) != null) {

        System.out.println(line);

    }

}

向你之前那种写法就可以了
不用写localhost和8080

这种访问方式需要确定端口,默认你访问的是80端口,如果tomcat没有使用80端口,你肯定是不行。

还有如果你使用这种方式,肯定是知道连接的,如果不知道的话,肯定也是访问不了的。 :arrow:

这个当然要端口了
不写端口的话 默认是80端口的

我就没见过你这样的写法,发送URL请求到那个控制层就行了(写相对路径) 你需要那个端口,request里面就有获取端口号的方法