【问题标题】:Spring @RequestMapping Ajax url does not matchSpring @RequestMapping Ajax url 不匹配
【发布时间】:2017-05-10 07:36:48
【问题描述】:

我正在尝试向我的 spring-handler 发送 get-request,但它不起作用。 我的处理程序:

@RequestMapping(value="/master_record/details")
public @ResponseBody String user(@RequestParam(value="user") String user_name,
                                 @RequestParam(value="password") String password) {
    return "/success";
}

还有我的 ajax 请求:

function toggleHistory(nuser, pw){

        $.ajax({
            type : "GET",
            url : 'master_record/details',
            data : {"user":  nuser, "password": pw},
            success : function(result) {
                 console.log("SUCCESS");
            },
            error : function(data, textStatus, errorThrown) {                   
                console.log("Error is ", textStatus, errorThrown);
            }
        });         
    };

当我查看链接时,我看到:

http://localhost:8080/webapp/master/master/record?user=me&password=secret&_=149440118200

我看到第三个参数和值:_=149440118200 那是哪个参数?

错误日志

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /webapp/master/master_record/details. Reason:
<pre>    Not Found</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                

</body>
</html>

【问题讨论】:

  • 你的控制器类有@Controller吗?
  • 不起作用是什么意思?
  • 是的,我的控制器类有@Controller。其他路线运作良好。我的意思是,我看到这条消息:GET localhost:8080/webapp/master/master/… 404 (Not Found)
  • 你能显示你的班级水平@RequestMapping,我认为这是导致问题的原因!?

标签: ajax spring request-mapping


【解决方案1】:

很明显你的网址是错误的

    function toggleHistory(nuser, pw){

    $.ajax({
        type : "GET",
        url : 'http://loclhoast:8080/master/record',
        data : {"user":  nuser, "password": pw},
        success : function(result) {
             console.log("SUCCESS");
        },
        error : function(data, textStatus, errorThrown) {                   
            console.log("Error is ", textStatus, errorThrown);
        }
    });         
};

当您将 url 设置为“master/record”时,它将采用明显位于 webapp/master 下的项目路径之后的路径。

您需要指定运行服务器的机器的完整 URL,在我的示例中,它的 localhost:8080 是 tomcat 的默认值。

【讨论】:

  • 当我尝试使用 'loclhoast:8080/master/record' 之类的 url 时,出现如下错误:请求的资源上不存在“Access-Control-Allow-Origin”标头。我想知道,因为我曾经使用简单的 url 部分,例如 url: 'newRoute' 并且它工作正常,但现在不行......
  • 这是一个不同的错误,该错误是关于拒绝来自不同域的请求的 CORS 协议,您可以使用 WebMvcConfigurer 在应用程序中为特定方法添加 CORS 映射
  • 对不起,我的意思是我试过这样的网址:localhost:8080/master/master_record/details 和 www.localhost:8080/webapp/master/master_record/details 但它仍然无法正常工作。我不知道为什么
  • 您是否尝试使用像 postman 这样的 http 客户端应用来请求这些 url?
  • 好主意。当我用这个 url 尝试它时:www.localhost:8080/webapp/master/master_record/details?user=me&password=secret&_=1494408977498 我将被重定向到登录页面。作为回应,我在邮递员中看到了 DOM 文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多