I am facing an issue with my JSP page where I have a button that calls an Ajax method named "sendmail()" upon click. The send mail API is in the Controller, and I am trying to display an alert message in the Ajax success function using data.message, but it shows that data is undefined.
I attempted to set the response object in the controller with a message string as 'success' and return it as a string. Please note that to achieve this, I had to change the return type of my controller's sendmail method to String. However, I noticed that the Ajax call does not enter the success function in this scenario. It works fine when the method has a void return type. Upon checking with the Chrome Developer tools, the network call returns a 404 error (not found). The email is sent successfully, but the Ajax success function is not triggered.
Below is the code snippet for the button and Ajax method:
<div class="uk-width-large-2-5">
<div class="uk-form-row">
<label>Message</label>
<textarea id="message" cols="30" rows="4" class="md-input"></textarea>
</div>
<div class="uk-form-row">
<button type="submit" class="md-btn md-btn-success md-btn-large" onclick="sendMail()">Send Message</button>
</div>
</div>
Ajax method :
<script>
function sendMail() {
var reqJson = {};
reqJson.msg = $("#message").val();
$.ajax({
type : "POST",
url : "sendMail",
data : JSON.stringify(reqJson),
dataType: 'json',
contentType: "application/json",
success : function(data) {
console.log("data :"+data.message);
}
error: function()
{
}
});
}
</script>
Maincontroller.java
@RequestMapping(value = "/sendMail", method = RequestMethod.POST, produces = "application/json")
public void sendContact(HttpServletRequest request, HttpServletResponse response, @RequestBody String payload) {
JSONObject jRespObj = new JSONObject();
try {
System.out.println("Welcome");
JSONObject jPayload = new JSONObject(payload);
System.out.println("jobj : "+jPayload);
String message = jPayload.getString("msg");
InputStream inputStream = UserController.class.getResourceAsStream("/Contactusrequest.htm");
StringWriter writer = new StringWriter();
try {
IOUtils.copy(inputStream, writer);
} catch (IOException e) {
e.printStackTrace();
}
HttpSession session = request.getSession(true);
String from = (String) session.getAttribute("email");
String to ="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1068696a507172733e737f7d">[email protected]</a>";
String emailContent = writer.toString();
emailContent = emailContent.replace("replaceMeFromEmail",from);
emailContent = emailContent.replace("replaceMeToEmail", to);
emailContent = emailContent.replace("replaceMeReason", message);
emailClient.sendMail("", to, null, "Contact Us Request", emailContent);
jRespObj.put("message", "Mail sent successfully");
response.setStatus(HttpServletResponse.SC_OK);
} catch (Exception ex) {
ex.printStackTrace();
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
return jRespObj.toString();
}
I am looking to receive the response object from the controller in the Ajax success callback to access the data.message.