0

I have a ajax call to insert some data into database. The below code is jquery get method.

$('#create').click(function(event) {
  var course = $('#course').val();
  var level = $('#level').val();
  var question = $('#question').val();
  var option1 =$('#option1').val();
  var option2=$('#option2').val();
  var option3=$('#option3').val();
  var option4=$('#option4').val();
  var answer=$('#answer').val();
  alert(course+" "+level+" "+question+" "+option1+" "+option2+" "+option3+" "+option4+" "+answer);
  $.get('AddQuestion', { course : course, level : level, question : question, option1 : option1, option2 : option2, option3 : option3, option4 : option4, answer : answer }, function(responseText) {
      /* if(responseText==="success"){
      alert("Question added");
      } 
      else{
      alert("Question is not added");
      } */
      alert(responseText);
   });
  cal();
});

In my java servlet code after data insertion i worte like below.

response.getWriter().append("success"); 

when i get response as success i would like to show a alert in jsp as data successfully inserted as shown in above commented code. But it isn't working.
How can i show alert box with success message ? any idea thanks in advance..

santosh
  • 5
  • 2

1 Answers1

0

Check what is returning responseText. Maybe is responseText.SOMETHING what you should put in the if.

Bitito
  • 191
  • 6
  • alert is not displaying in jsp. when i write like this. alert(responseText); in $.get() method. – santosh Jun 11 '17 at 04:45
  • Try to log it, if it is not a String, alert wont show it! (console.log()) – Bitito Jun 11 '17 at 04:47
  • I added console.log(responseText); inside $.get() method and added some other text inside it. But it's not calling. – santosh Jun 11 '17 at 12:16
  • response.getWriter().write("success"); I have to call write method instead of append method. Found the answer. – santosh Jun 11 '17 at 16:02