request method 'post' not supported问题解决

左手的ㄟ右手 2022-06-05 08:23 631阅读 0赞

  最近做j2ee开发时,使用spring框架,用jquery的ajax方法提交表单时,遇到了request method ‘post’ not supported这个警告而且数据传送不成功,于是在百度上搜了一下,可能是表单提交时没有填写数据导致的以及其他可能的原因,我调试了一天,还是未解决这个问题。于是问了项目经理,他一看我的代码就说这个是url的问题吧,我看了一下原来url写了相对路径,这个就无法提交,于是获取前面加了base标签:数据发送成功,在此记录一下以免以后再犯。   

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <base href="<%=basePath%>">
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Insert title here</title>
  10. <script src="js/jquery-3.2.1.js"></script>
  11. <!-- 新 Bootstrap4 核心 CSS 文件 -->
  12. <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
  13. <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
  14. <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  15. <!-- popper.min.js 用于弹窗、提示、下拉菜单 -->
  16. <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
  17. <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
  18. <script src="https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
  19. </head>
  20. <body>
  21. <div align="center">
  22. <form action="login" onsubmit="return false;" id="userForm" method="post">
  23. <table class="table table-hover">
  24. <thead>
  25. <tr>
  26. <input type="text" name="name" placeholder="登录账号" />
  27. </tr>
  28. </thead>
  29. <tbody>
  30. <tr>
  31. <input type="password" name="pwd" placeholder="登录密码" />
  32. </tr>
  33. <tr>
  34. <button type="button" class="btn" onclick="userLogin()">基本按钮</button>
  35. </tr>
  36. </tbody>
  37. </table>
  38. <br>
  39. </form>
  40. </div>
  41. <script type="text/javascript"> function userLogin() { $.ajax({ type : "post", url : "login", data : $("#userForm").serialize(), dataType : "json", success : function(data) { alert(data.flag); if (data.flag == "ok") { location.href = "index"; } else { alert("登录失败!"); } } }) } </script>
  42. </body>
  43. </html>

主要是没有加

  1. <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>

  1. <base href="<%=basePath%>">

  加上以后,问题解决。记得base标签放head第一行


  这个世界上没有知识是学不会的,不是吗?如果一开始学不会,就可以把问题细化分解,然后学习更基本的知识。最后,所有问题都能变得和1+1=2一样简单,我们需要的只是时间。好了,最后给大家推荐一个学习Java的好网站 JAVA自学网站–how2j.cn

发表评论

表情:
评论列表 (有 0 条评论,631人围观)

还没有评论,来说两句吧...

相关阅读