-1

I'm posting data to Servlet by using ajax. And I don't know how to GET result from Servlet. Please help me to solve this problem. I want to get Result from Servlet. If its success redirect to Success page, if its Failed, redirect to failed page.

Ajax getting response "success" but not redirecting to another page. this is response

This is what I have done till now:

<script>
$(document).ready(function() {                       
    setInterval("ajaxd()",3000);

});

function ajaxd() { 

$.ajax({
  url:'/Query.cfm',
  data:{amount:'<%=amount%>', No:'<%=No%>', eno:'<%=No%>', fno:'<%=fno%>', sign:'<%=sign%>'},


  type:'get',
  cache:false,
  success:function(data){
     if (data == 'success') {
         var u = '/notic/succ.jsp?No=<%=No%>&amount=<%=amount%>';
         alert(u);
         window.location.href = u;
     }else if(data == 'failed'){
         window.location.href = '"/notic/failed.jsp?No="<%=No%>"&amount="<%=amount%>';
     }
   },
  }
 );
}

</script>

This is my Servlet's response: Result: can be "success" or "failed"

response.setContentType("text/plain");
response.getWriter().println(Result);
Abdulin
  • 7
  • 6
  • Can you provide your Servlet code as well? You probably need to write success / failed to the ServletResponse outputstream. – C. Smith Dec 03 '16 at 02:46
  • Ok. I would put a `console.log(data);` in your javascript inside the `success` function i order to see what `data` value is. Also add an `error` and `done` functions below `success` to catch and log errors. Since you're not returning json you could also try adding `'dataType:text'` to your `ajax` call. – C. Smith Dec 03 '16 at 03:09
  • Try `window.location=u;` instead of `window.location.href=u;` – C. Smith Dec 03 '16 at 03:12
  • @C.Smith its not jumping to another page still... – Abdulin Dec 03 '16 at 03:22
  • does `alert(u);` show anything, if so what? if you look in your browser console do you see anything logged? – C. Smith Dec 03 '16 at 03:24
  • Also you could try this, since you're already using jQuery: `$(location).attr('href',u);` – C. Smith Dec 03 '16 at 03:26
  • `alert(u);` doesnt show anything... What do you think whats the problem? @C.Smith – Abdulin Dec 03 '16 at 03:30
  • if `alert(u);` never fires then its likely not making it into your success function. or are you saying the alert is empty, which seems hard to fathom since it is set immediately prior to the alert. – C. Smith Dec 03 '16 at 03:34
  • @C.Smith `Thanks a lot, I really appreciate your help! – Abdulin Dec 03 '16 at 03:36

1 Answers1

1

Problem was: due to using println() instead of

print().
Abdulin
  • 7
  • 6
Rocky Sims
  • 3,523
  • 1
  • 14
  • 19
  • It that doesn't fix it, perhaps you could add – Rocky Sims Dec 03 '16 at 03:09
  • console.log(data); after if (data == 'success') and post here what data is – Rocky Sims Dec 03 '16 at 03:11
  • I wish I could edit my comments... I meant to say: If that doesn't fix it, perhaps you could add console.log(data); before if (data == 'success') and post here what data is – Rocky Sims Dec 03 '16 at 03:14
  • Im getting success for console.log(data); – Abdulin Dec 03 '16 at 03:19
  • From the picture you posted it looks like your getting "success" with a new line after it (the "\n" isn't a visible character and shows up as an extra blank line). response.getWriter().println(Result); to response.getWriter().print(Result); I'll bet that'll fix it. – Rocky Sims Dec 03 '16 at 03:34
  • I agree this is worth a shot. Please try this and report on the results. Based on your previous comment, if the `alert(u)` is not firing then `data == 'success'` is not true – C. Smith Dec 03 '16 at 03:38
  • Yeah, worked, thanks a lot. I really appreciate it! – Abdulin Dec 03 '16 at 03:39