2

I have a simple function which only returns a translated message from the server to the client. But the result shows undefined, when I pass the result into a var.

function MessageNoResult() {
  $.ajax(
  {
    type: "POST",
    async: true,
    url: '<%= ResolveUrl("~/WebMethods.aspx/MessageNoResult") %>',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      return msg.d;
    }
  });
}

Result --> Undefined (bad)

var message = MessageNoResult();
alert(message); 

When I look into the Headers it gives me:

Server  ASP.NET Development Server/9.0.0.0
Date    Wed, 09 Nov 2011 09:01:31 GMT
X-AspNet-Version    2.0.50727
Cache-Control   private, max-age=0
Content-Type    application/json; charset=utf-8
Content-Length  24
Connection  Close

Response {"d":"No search result"}
JSON d "No search result"

Why do I still get an undefined result?

4
  • you are trying to return from callback function. one of the ways is to do your code with response in success block Nov 9, 2011 at 9:13
  • what does the console say when you type console.dir(msg) within you success: function(msg)? Nov 9, 2011 at 9:14
  • When I place the code into the success block, it works perfect. I was trying to make the code more reusable.
    – Colin
    Nov 9, 2011 at 9:24
  • 1
    In that case you will need to place the logic which deals with your ajax response in its own function and call that from your success handler - see the answers from me and AndreasAL for how to do this. Nov 9, 2011 at 9:31

5 Answers 5

3

You can pass a callback function to the MessageNoResult function

function MessageNoResult(callback) {
  $.ajax(
  {
    type: "POST",
    async: true,
    url: '<%= ResolveUrl("~/WebMethods.aspx/MessageNoResult") %>',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      callback(msg);
    }
  });
}

And then call:

MessageNoResult(function(msg) {
    alert(msg.d);
});
2

The ajax call is still being executed as your code moves on to the alert(message) line, so the message variable has no value.

You need to pass the value of the response to a function within the success method.

success: function(msg) {
  myFunction(msg.d);
}

// outside ajax call
function myFunction(ajaxValue) {
    alert(ajaxValue)
}
2
  • There is no way to specifically set a variable, as you will still encounter the same problem where process flow will continue before your ajax has fully executed. You only real option if you don't want to place your code in the success method is to pass it across to another function - example amended. Nov 9, 2011 at 9:19
  • 1
    Expanded Rory McCrossan's answer with a callback: stackoverflow.com/questions/8062574/… Nov 9, 2011 at 9:22
0

The ajax call is an async call and you’re running the alert right after the MessageNoResult, thus the message has no value until you’ve actually completed the ajax request.

You’ll need to run your alert inside the success from ajax call.

0

You are trying to return from success which is a callback function. Set a global variable and use it within the success function retmess = msg.d to store the return or set the content of an html element jQuery('#status').html(msg);

1
  • 1
    This will give you the problem that you don't know when the value of retmess gets set so you won't know whne you can use it.
    – njr101
    Nov 9, 2011 at 9:25
0

Your code:

var message = MessageNoResult();

is setting the message variable to the return value of the MessageNoResult functiion. Since you have no return value in this function, you get undefined as response.

The corrcet way to handle this is by dealing with the return value from the Ajax call inside the success callback.

For a similar question see my answer here:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.