我正在尝试在ajax调用的帮助下在webservice中发送我的html表单数据。我需要将它们保存在变量中,并将它们放在模板中,以便通过电子邮件发送。请帮帮忙。我对Java编码是个新手。另外,我是否需要在web.xml等文件中添加我的etc服务名称?帮助我在我的服务中获取数据。
我的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);
}
发布于 2018-06-25 04:47:39
删除< form action="./AskForDemo“method="POST”class=“表单面板-body”role=“表单”>
只需编写< form action="“method="post”class="form panel-body“role=”>即可
您已经在Ajax调用时在URL中指定了Action。试试这个也许能行得通。
https://stackoverflow.com/questions/51010411
复制相似问题