gpt4 book ai didi

java - Ajax 请求始终映射到根

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:56 26 4
gpt4 key购买 nike

即使指定了完整路径(如“/ajax/file/upload”),我的 ajax 请求也始终映射到“/”

在我的 Controller 中,我有以下两种方法

@RequestMapping(value = "/")
public ModelAndView getView() {
}

@RequestMapping(value = "/ajax/file/upload", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
@ResponseBody
public String readFile(File file) {
}

在客户端,当我实例化一个像“ajax/file/upload”这样的ajax请求时,该请求已被重定向到方法“getView”而不是“readFile”。

我认为,发生这种情况是因为请求映射“/”也匹配模式“ajax/file/upload”,但不确定。如果是这样,如何区分这两个映射?

最佳答案

I think, this happen because the request mapping "/" also matches the pattern "ajax/file/upload"

Spring 将始终使用最佳匹配来决定哪个方法应该处理请求。

可能会发生一些事情:

  • 如果在客户端发送 GET 请求而不是 POST 请求,则最佳匹配是“/”,因为“/ajax/file/upload”仅接受 POST 请求。您可以在浏览器网络 Activity 中对其进行调试,以检查请求/响应的详细信息
  • 如果您实现了安全措施,则需要检查您的 ajax 请求是否发送了正确的 cookie/身份验证 token

另一件可能值得检查的事情是,您的 ajax 请求是否发送 header “Accept: application/json”或“Accept: /

关于java - Ajax 请求始终映射到根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com