request method 'post' not supported问题解决
最近做j2ee开发时,使用spring框架,用jquery的ajax方法提交表单时,遇到了request method ‘post’ not supported这个警告而且数据传送不成功,于是在百度上搜了一下,可能是表单提交时没有填写数据导致的以及其他可能的原因,我调试了一天,还是未解决这个问题。于是问了项目经理,他一看我的代码就说这个是url的问题吧,我看了一下原来url写了相对路径,这个就无法提交,于是获取前面加了base标签:数据发送成功,在此记录一下以免以后再犯。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.2.1.js"></script>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<!-- popper.min.js 用于弹窗、提示、下拉菜单 -->
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
</head>
<body>
<div align="center">
<form action="login" onsubmit="return false;" id="userForm" method="post">
<table class="table table-hover">
<thead>
<tr>
<input type="text" name="name" placeholder="登录账号" />
</tr>
</thead>
<tbody>
<tr>
<input type="password" name="pwd" placeholder="登录密码" />
</tr>
<tr>
<button type="button" class="btn" onclick="userLogin()">基本按钮</button>
</tr>
</tbody>
</table>
<br>
</form>
</div>
<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>
</body>
</html>
主要是没有加
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
和
<base href="<%=basePath%>">
加上以后,问题解决。记得base标签放head第一行
这个世界上没有知识是学不会的,不是吗?如果一开始学不会,就可以把问题细化分解,然后学习更基本的知识。最后,所有问题都能变得和1+1=2一样简单,我们需要的只是时间。好了,最后给大家推荐一个学习Java的好网站 JAVA自学网站–how2j.cn
还没有评论,来说两句吧...