jqueryajaxresponsetext

Need the responseText of jQuery.ajax returned instead of using the 'success' function


I would like to have a function that returns the repsonseText of a jQuery.ajax() call. All the examples I've seen say to use a 'success' function to handle the returned data. However, for my implementation I need something like following:

function getRemoteValue(id) {
  var request = jQuery.ajax({
    url:'somefile.php',
    dataType:'text'
  });
  return request.responseText;
}

When I make a call to this function, Firebug shows the request as going through with the correct Response being returned. However when I try the following, I only get an empty string:

var some_value = getRemoteValue(1); // The problem is here. some_value is empty.
jQuery('.someclass').html(some_value);
// Other processing using some_value;

Again, for my implementation I can't be doing the jQuery('.someclass').html(some_value); within the ajax() call. How can I get the responseText returned? Thank you!


Solution

  • Rober ,

    The following code is valid but returns null

    var some_value = getRemoteValue(1); 
    

    The ajax call is asyncronous call , it just instantiates the process goes on , so you will always have this problem

    You need to move your code to your success handler of ajax to do any post operations.