标准项目结构。使用JSP,JQuery和Spring框架。
问题很简单,AJAX调用没有按预期运行。我希望将输入值发送到支持,并在浏览器中向最终用户显示警报“成功”。
但是,这种情况表现得好像Jquery调用不存在一样。
JS资源位于:
WEB-INF /资源/ JS
JSP:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Lorem Ipsum</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/js/subscribe.js" type="text/javascript"></script>
<link href="${pageContext.request.contextPath}/resources/css/style.css" rel="stylesheet" >
</head>
<body>
<div class="jumbotron text-center">
<form class="form-inline" action="/intuition/subscribe" method="post">
<div class="input-group">
<input type="email" name="email" class="form-control" size="50" placeholder="Email Address" required>
<div class="input-group-btn">
<button type="submit" class="btn btn-success">Subscribe</button>
</div>
</div>
</form>
</div>
</body>
</html>
subscribe.js
$(document).ready(function() {
/* attach a submit handler to the form */
$("#form-inline").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get the action attribute from the <form action=""> element */
var $form = $( this ),
url = $form.attr( 'action' );
/* Send the data using post with element id name and name2*/
var posting = $.post( url, { name: $('#email').val() } );
/* Alerts the results */
posting.done(function( data ) {
alert('success');
});
});
});