qq_26290227
蓝胧月
2020-11-17 10:16
采纳率: 66.7%
浏览 85

ajax请求controller,页面报错404,请大佬看一下URL怎么写

ajax请求controller,页面报错404,debug调试发现后台未执行,请大佬看一下URL怎么写。

js

function fileDownload(fileName) {
    var dataRow = {
        dateTime: new Date(),
        userGroup: 'admingroup',
        user: 'admin',
        eventType: "文件上传下载",
        operation: "下载",
        controlDesc: "下载" + fileName
    };
    $.ajax({
        url: "<%=request.getContextPath()%>/operating/addLog.do",
        type: "POST",
        data: JSON.stringify(dataRow),
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        success: function (result) {
            console.log(result);
        },
        error: function () {
            console.log("error");
        }
    })
}

controller

@Controller
@RequestMapping("/operating")
public class OperatingController {
    @Autowired
    private OperatingService operatingService;

    @RequestMapping(value = "/addLog.do",method = RequestMethod.POST)
    @ResponseBody
    public String addOperatingRecords(@RequestBody OperatingRecords operatingRecords) {
        if (operatingService.addOperatingRecords(operatingRecords)) {
            return JSON.toJSONString("success");
        }
        return JSON.toJSONString("fail");
    }

}

OperatingRecords

public class OperatingRecords {
    Date deteTime;
    String userGroup;
    String user;
    String eventType;
    String operation;
    String controlDesc;
    
    //省略无参构造和get、set
}

项目结构

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • qq_25376897
    qq_25376897 2020-11-17 11:17
    已采纳
    点赞 评论
  • qq_38167579
    农夫丶果园 2020-11-17 10:43

    首先要到浏览器的控制台看看到底是哪个url报的404 , 我记得request.getContextPath() 好像是以 / 结尾的 ,  /operating/addLog.do的第一个 / 应该可以不要 , 你试试

    点赞 评论
  • wildcata
    离狮 2020-11-17 11:41

    URL可以写到页面中的<input type='hidden' href=''>,然后通过jQuery获取input的href,填到URL里。

     

    点赞 评论

相关推荐