首页 > 解决方案 > 错误 404:ajax 无法连接到 @requestmapping

问题描述

我的 ajax 无法连接到这里,当我看到 F12->Network->Headers 我看到 requestUrl 有双“/web”:http://localhost:8080/create_user_web_war_exploded/web/web/done 但我的删除页面只有 1“ /web" 在链接 @RequestMapping(value = "/web/delete",method = RequestMethod.GET)。我认为在我的网站上出现错误 404,因为所有其他页面的链接都是“/'project_name'/'Request value'”

//My controller
@RestController
@RequestMapping(value = "/web/done")
public class UserController {
    @Autowired
    private IUserService userService;

    @DeleteMapping
    public String delete( @RequestBody UserDTO model,@PathVariable("id") long id){
        ModelAndView mav = new ModelAndView("web/done");
        userService.deleteUser(model.getId());
        return "done";
    }

}
//My ajax
function deleteUser(data) {
    console.log("hihi");
    console.log("done");
    $.ajax({
        type: 'DELETE',

        url: 'web/done',
        data: data,
        /*dataType:'json',*/
        /*contentType:'application/json',*/
        success: function () {
            console.log("done");
        },


    }).done(function (response) {
        alert("Job done!!!!");
    }).fail(function (xhr, status, error) {
        alert(xhr.responseText);
    });

标签: javaajaxspringspring-mvc

解决方案


控制器的主要映射是“/web/done”,但在主要方法上,您只调用“web/done”而没有第一个 /。尝试在 Ajax 上添加 /


推荐阅读