1

I have below ajax code which i want to print value on html page.

var val = $.ajax({
              type: "post",
              url: "count.jsp",
              data: "",
              cache: false,
              success: function(str)
              {
                return str;
              }
         });

         alert (val);

I want to use the val to print. count.jsp code is below.

ResultSet rs=pst.executeQuery();
if(rs.next())
{
    val=rs.getInt(1);
}
out.println(val);

Any advice how to print val on page?

Vivek Dhiman
  • 1,967
  • 6
  • 46
  • 82

1 Answers1

2

ajax is asynchronous. That means it finishes some time AFTER the ajax function itself completes. So you can't just simply return a value from your success handler and expect that to be returned from the ajax function.

In fact, returning a value from the success handler does absolutely nothing. The value just goes back into the internals of the ajax infrastructure.

What you need to do is to put your alert inside the success handler. Any code that wants to use the result of the ajax call must be in the success handler or called from the success handler:

$.ajax({
    type: "post",
    url: "count.jsp",
    data: "",
    cache: false,
    success: function(str) {
        // put code here to use the ajax data
        // or call a function here and pass it the ajax data
        alert(str);
    }
});

Further, per the jQuery documentation the return value from the jQuery .ajax() function is a jqXHR object.


And, the more modern way to use $.ajax() is to use promises. The jqXHR object it returns is also a promise. The general concept is this:

 $.ajax(...).then(result => {
     console.log(result);
 }).catch(err => {
     console.log(err);
 })
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Hi Thanks, str giving me output. But when trying to display on element like success: function(str) { //document.write(str); document.getElementById("test").value=str} } itsnot setting the value on
    – Vivek Dhiman Jan 24 '14 at 18:27
  • `document.getElementById("test").value = str` from inside the success handler should work just fine if you have a div with `id="test"`. Don't use `document.write()` from within an ajax call because that clears your entire document. – jfriend00 Jan 24 '14 at 18:39
  • its working now but
    Rate this Site :
    setting the value in next line how to make it in same line. I am using document.getElementById("test").innerHTML= str
    – Vivek Dhiman Jan 24 '14 at 19:47
  • @Champ23 - it sounds like you're asking an entirely new question about your HTML formatting. You should create a new question for that (hint a `div` is a block element - perhaps you want a `` which is an inline element). Also, if this answer solved your original question, please follow the StackOverflow guidelines and click the green checkmark to the left of it to show the SO community that your original question is solved. – jfriend00 Jan 24 '14 at 19:49
  • Hey Thanks, not able to see green checkmark how to mark solved. – Vivek Dhiman Jan 24 '14 at 20:10