一、绝对路径:包括协议名称、主机地址、端口、web项目名称等的完整请求路径。

例如:

$.ajax({ url:"http://localhost:8080/webname/test" });

好处:比如在webA项目中的ajax需要请求webB项目中的服务,则必须使用绝对路径。
坏处:使用绝对路径要求不能更改web项目的名称,如果webB项目重命名了,则对应的ajax请求都需要修改;往往项目开发调试和部署时run的环境不同,路径不同,不可能每次发布或调试把项目中路径全部修改一遍。

二、相对路径:不需要协议名、主机地址、端口、web项目名称,只需要请求的路径。

假设:

项目路径:http://localhost:8080/webname
页面路径:/webname/index.html(A页面),/webname/test/test.html(B页面)
请求路径:/request/ajaxtestrequest/ajaxtest
1、如果请求路径以根路径开头(常见),则无论什么ajax在什么页面,该请求都是相对于服务器的根路径,最后的请求路径都是:http://localhost:8080 + /request/ajaxtest(url地址)
例如:

$.ajax({
    url :"/request/ajaxtest"
});

原因:以"/"开头,是表示该请求基于从服务器的根路径,即不是相对于html的路径,所以请求路径是:http://localhost:8080 + /request/ajaxtest
2、如果请求不以根路径开头,则该请求路径是相对于当前html所在的路径的。
(1)假如请求在A页面( …/webname/index.html)

$.ajax({
        url:"request/ajaxtest"

    });

最终的请求路径是:http://localhost:8080/webname/request/ajaxtest
原因:index.html页面对应的路径是"/webname/index.html",所以将url在这个路径下就是最终的请求路径。
(2)假如请求在B页面(…/webname/test/test.html)

$.ajax({
        url:"request/ajaxtest"

    });

最终的请求路径是:http://localhost:8080/webname/test/request/ajaxtest。 原因:test.html页面对应的路径是“/webname/test/”,所以这里url要跟在test这一级下面。

备注:1、2两种方式在项目中使用基本使用 方式1。比如我们的登陆页面 login.html 是在 web(有的是Webcontent)目录下,这时我调一个接口直接使用1、2两种方式都可以访问后台;但是业务页面肯定会新建文件夹,那么就要使用“方式1”了,如果使用“方式2”的方式,登陆地址会加上当前页面的地址 比如: http:localhost:8080/项目名/ 。。。(html地址)/ 接口地址,显然访问不到后台了。

举例1:如下图项目结构,在web根目录下ajax.html想调用src/Servlet/AjaxServlet(url以’/'开头);

ajax.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-2.1.0.min.js"></script>
    <script>
        
        //定义方法
        function  fun() {
            //使用$.ajax()发送异步请求
            $.ajax({
                url:"/ajaxServlet" , // 请求路径(如果url使用'/'开头表示使用web根目录,即“http://localhost:8080/”,可以直接调用servlet)
                type:"POST" , //请求方式
                data:{"username":"jack","age":23},
                success:function (data) {
                    alert(data);
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误,会执行的回调函数
                dataType:"text"//设置接受到的响应数据的格式
            });
        }
        
    </script>
</head>
<body>

    <input type="button" value="发送异步请求" onclick="fun();">

    <input>
</body>
</html>

AjaxServlet.java

@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数
        String username = request.getParameter("username");

        //2.打印username
        System.out.println(username);

        //3.响应
        response.getWriter().write("hello : " + username);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

此时,在浏览器输入http://localhost:8080/ajax.html再点击按钮是可以调用到servlet的:

举例2:如下图项目结构,在web根目录下ajax.html想调用src/Servlet/AjaxServlet(url不以’/'开头);

ajax.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-2.1.0.min.js"></script>
    <script>
        
        //定义方法
        function  fun() {
            //使用$.ajax()发送异步请求
            $.ajax({
                url:"ajaxServlet" , // 请求路径(如果url不使用'/'开头表示使用当前html文件所在的目录为相对路径,当前路径为“http://localhost:8080/”,可以直接调用servlet)
                type:"POST" , //请求方式
                data:{"username":"jack","age":23},
                success:function (data) {
                    alert(data);
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误,会执行的回调函数
                dataType:"text"//设置接受到的响应数据的格式
            });
        }
        
    </script>
</head>
<body>

    <input type="button" value="发送异步请求" onclick="fun();">

    <input>
</body>
</html>

AjaxServlet.java

@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数
        String username = request.getParameter("username");

        //2.打印username
        System.out.println(username);

        //3.响应
        response.getWriter().write("hello : " + username);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

此时,在浏览器输入http://localhost:8080/ajax.html再点击按钮是可以调用到servlet的:

举例3:如下图项目结构,在web根目录下hhh文件夹里的ajax.html想调用src/Servlet/AjaxServlet

ajax.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-2.1.0.min.js"></script>
    <script>
        
        //定义方法
        function  fun() {
            //使用$.ajax()发送异步请求
            $.ajax({
                url:"../ajaxServlet" , // 请求路径(如果url不使用'/'开头表示使用当前html文件所在的目录为相对路径,当前路径为“http://localhost:8080/hhh/”,需要使用../回退到web根目录http://localhost:8080/,再调用servlet)
                type:"POST" , //请求方式
                data:{"username":"jack","age":23},
                success:function (data) {
                    alert(data);
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误,会执行的回调函数
                dataType:"text"//设置接受到的响应数据的格式
            });
        }
        
    </script>
</head>
<body>

    <input type="button" value="发送异步请求" onclick="fun();">

    <input>
</body>
</html>

AjaxServlet.java

@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数
        String username = request.getParameter("username");

        //2.打印username
        System.out.println(username);

        //3.响应
        response.getWriter().write("hello : " + username);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

此时,在浏览器输入http://localhost:8080/hhh/ajax.html再点击按钮是可以调用到servlet的:

更多推荐

Ajax请求中url的绝对路径和相对路径