[英]Not able to send html form ajax data in java webservice
我正在尝试通过ajax调用在Web服务中发送html表单数据。 我需要将它们保存在变量中,并将其放在模板中以通过电子邮件发送。 请帮忙。 我是Java编码的新手。 我还需要在任何文件中添加我的Web服务名称,例如web.xml等吗? 帮助我在服务内部获取数据。
我的html表单代码是:
<form action="./AskForDemo" method="POST" class="form panel-body" role="form">
<div class="form-group">
<input class="form-control" id="name" name="name" autofocus placeholder="Your Name" type="name" />
</div>
<div class="form-group">
<input class="form-control" id="emailID" name="emailID" autofocus placeholder="Your e-mail" type="email" />
</div>
<label for="date1"> Please suggest any three dates with time you are available for a demo</label>
<div class="form-group">
<input class="form-control" id="date1" name="date1" type="datetime-local" />
</div>
<div class="form-group">
<input class="form-control" id="date2" name="Date2" placeholder="MM/DD/YYYY" type="datetime-local" />
</div>
<div class="form-group">
<input class="form-control" id="date3" name="date3" placeholder="MM/DD/YYYY" type="datetime-local" />
</div>
<div class="form-group">
<textarea rows="4" class="form-control" name="message" id="message" required placeholder="our demo" rows="5"></textarea>
</div>
<button class="btn btn-primary pull-right" type="submit" onClick="askfordemo({test:'ok'})">Send</button>
</form>
我的JavaScript代码是:
function askfordemo(){
var path = document.URL;
var frm = $(document.forms);
var formData = JSON.stringify(frm.serializeArray());
var emailID = document.getElementById("emailID").value;
var name = document.getElementById("name").value;
var date1 = document.getElementById("date1").value;
var date2 = document.getElementById("date2").value;
var date3 = document.getElementById("date3").value;
var message = document.getElementById("message").value;
path = path.substring(0, path.lastIndexOf("/"));
$.ajax({
url : path + '/ms/askForDemo/sendMail?emailID='+emailID,
type: 'POST',
data: formData,
dataType: "json",
crossDomain: 'true',
success: function(formData){alert (formData);},
error: function(x, y, z) {
// alert(x.responseText + " " + x.status);
alert("Internet connection is lost. Please try again later.");
}
});
我的网络服务:
@Path("/askForDemo")
public class AskForDemo {
private static Logger logger = Logger.getLogger(AskForDemo.class);
private String fromUser;
private String host;
private String authenticate;
private String smtpPort;
private String smtpPwd;
private String smtpLogin;
private String from;
@POST
@Path("/sendMail/{emailID}")
@Produces("text/plain")
public void sendMail(@PathParam("emailID") String emailID,HttpServletRequest
request, HttpServletResponse response){
String subject = "Ask for demo";
String name = request.getParameter("name");
String email = request.getParameter("email");
String date1 = request.getParameter("date1");
String date2 = request.getParameter("date2");
String date3 = request.getParameter("date3");
String message = request.getParameter("message");
sendMailToSales(name, email,date1,date2,date3,message);
}
删除<form action =“ ./ AskForDemo” method =“ POST” class =“ form panel-body” role =“ form”>
只需编写<form action =“” method =“ post” class =“ form panel-body” role =“ form”>
您已经在Ajax调用中在URL中提供了操作。 试试这个可能会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.