在前后端使用ajax进行数据交互的时候,需要在前端代码中编写URL路径进入后端代码的controller中,从而获取数据。初次尝试前后端交互也许会遇到困难,在此,我列举了几种URL的写法供大家分享。
$ajax({ url:"/getData.do"; })
该方式请求的路径为:http://localhost:8080/getData.do
$ajax({ url:"../getData.do; })
该方法请求的路径是:http://localhost:8080/projectname/getData.do
$ajax({ url:"getData.do"; })
该方式的请求路径:http://localhost:8080/projectname/getData.do
$ajax({ url:"http://localhost:8080/projectname/getdata.do"; })
该方式的请求路径:http://localhost:8080/projectname/getdata.do
.do的含义
比如你的页面路径是:http://localhost:8080/projectname/resource/index.html
url请求最后加.do是为了服务器区分这个请求是静态资源还是servlet请求(后边有.do就是servlet请求)
@RequestMapping("/user") @Controller public class UserController{ @RequestMapping("/register") public @ResponseBody JsonMessageObject register(@RequestBody UserInfoPo userInfoPo){
如果我们要请求register这个方法,url:…/user/register.do这样写
因为页面路径是这样http://localhost:8080/projectname/resource/index.html
让servlet映射请求跟在项目路径http://localhost:8080/projectname后面就可以了
最终请求的路径是http://localhost:8080/projectname/user/register.do