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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章