简体   繁体   中英

Not able to send html form ajax data in java webservice

I am trying to send my html form data in webservice with the help of ajax call. I need to save them in variables and put them in template for emailing purpose. Please help. I am new to Java coding. Also do i need to add my webservice name in any files like web.xml etc? Help me to get data inside my service.

My html form code is:

<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>

My javascript code is:

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.");
    }
    });

My webservcie:

@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);
}

Remove the < form action="./AskForDemo" method="POST" class="form panel-body" role="form">

Just write < form action="" method="post" class="form panel-body" role="form">

You already given Action in URL at Ajax call. Try this may be it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM