[英]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.