views:

37

answers:

2

This function will be using in general ajax calss:

function f_AjaxFunction(_param) {
    var objectWillReturn;
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: 'WS/wsGenral.asmx/f_QueryAndGetResultAsJson',
        data: "{_sParam:'" + _param + "'}",
        dataType: "json",
        success: function(data) {
            var txt = "";
            try {
                objectWillReturn = data;
            }
            catch (err) {
                alert(err.description);
            }
        }
    });
    return objectWillReturn ;
}

This function is calling the other function which is above:

    function f_HavaDurumu(_paramm) {
        var obj;
        obj = f_AjaxFunction(_paramm);
        $("#spanShow").html(obj.d);
    }
+3  A: 

You will have to use callbacks since the calls are made asychronously:

function f_AjaxFunction(_param, callback) {
  $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: 'WS/wsGenral.asmx/f_QueryAndGetResultAsJson',
      data: "{_sParam:'" + _param + "'}",
      dataType: "json",
      success: function(data) {
          var txt = "";
          try {
              if($.isFunction( callback(data) )){
                callback(data);
              }
          }
          catch (err) {
              alert(err.description);
          }
      }
  });
}

function f_HavaDurumu(_paramm) {
    f_AjaxFunction(_paramm, function(data){
      $("#spanShow").html(data.d);  
    });
}

I can't think of a situation where your code would cause an error in the Try block, but I left it in because you had it there to begin with.

UPDATE

To illustrate this further:

function get_ajax(){
    var ran = false;
    $.ajax({ 'url':'/some/url', success: function(data){
        ran = true;
    });
    return ran;
}

alert(get_ajax()); // alerts "false"

It is running in this order:

  1. Set run to false
  2. Start AJAX request
  3. Return run
  4. AJAX finished, set run to true

However, if you use callbacks, you can keep things in order so they execute like this:

  1. Start AJAX Request
  2. AJAX finished, execute the callback with the return data
Doug Neiner
+1 Didn't know about `$.isFunction`.
Alex Bagnolini
How can i set the return object to the new one where i called the function? Can't i set => var newObj = f_AjaxFunction("hihi");
uzay95
No, the function returns immediately after calling `$.ajax` because the call is asynchronous and is pulled out of the current execution queue. The rest of your call has to be handled in the callback event or it will execute *before* the AJAX call returns. Make sense?
Doug Neiner
Thank you very much.
uzay95
@uzay95 I updated my answer with a little more detail. Good luck on your project!
Doug Neiner
A: 

As Doug said, you will need to use asynchronous callbacks. f_AjaxFunction is returning before the data is available.

Another alternative is Javascript Strands.

Dark Falcon
+1 Cool link... never looked at Strands before!
Doug Neiner