0

I'm looking to return the value from an ajax call which is inside a jquery function. I have this code but it returns undefined. Any help as to what I'm doing wrong is much appreciated.

$.inlDo = function(action,rec,val) {
    $.ajax({
        type: 'POST', url: 'editFile.php?do='+action+'&record='+rec+'&val='+val,
        success: function(d) {
            return 'ok';
        }
    });
}

alert($.inlDo('del',id,''));

The ajax call is successful so that's not likely to be the problem.

greener
  • 4,989
  • 13
  • 52
  • 93

3 Answers3

2

You cannot return a value using a ajax call

$.inlDo = function(action,rec,val) {
    $.ajax({
        type: 'POST', url: 'editFile.php?do='+action+'&record='+rec+'&val='+val,
        success: function(d) {
          alert(d);
       //i will suggest you to add a function call back
         compltedRequest(d);
        }
    });
}

function completdRequest(data)
 {
  alert("request has been completed");
 }
StaticVariable
  • 5,253
  • 4
  • 23
  • 45
1

Try like this:

$.inlDo = function(action,rec,val) {
    return $.ajax({
        async: false,
        type: 'POST', url: 'editFile.php?do='+action+'&record='+rec+'&val='+val,
        success: function(d) {
            return 'ok';
        }
    }).responseText;
}

If you are reciving an XML or a JSON response, you should use some XML parser, or JSONIFY for a JSON response so you could access the response as an object

StaticVariable
  • 5,253
  • 4
  • 23
  • 45
  • This was helpful. For me, the "ok" value had to go into `editFile`. Cheers – greener Oct 12 '12 at 17:11
  • `async: false` is almost always a Very Bad Idea. This is definitely one of those occasions. It's the quick, easy and wrong way out. – lonesomeday Oct 12 '12 at 17:15
  • please check the jQuery docs, before you make a statement on my research result :) or provide non jQXHR examples how @greener can achive same results. Cheers. –  Oct 12 '12 at 17:17
  • @Zlatan frozenkoi's answer gives the reason for the problem; jhonraymos gives the solution. This is a problem that has been answered dozens of times on SO, for instance [here](http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success). – lonesomeday Oct 12 '12 at 17:20
  • @lonesomeday yea, right. I've provided an example where the entrie response is "returned", and the instance you've sent handles the response via other function/handler/whatever. Guy was trying to: alert($.inlDo('del',id,'')); and I've just explain how to (do that)! :) –  Oct 12 '12 at 17:22
  • @Zlatan The point is that doing that ignores the whole purpose and conventions that underlie AJAX. The OP only wants to return the data because he/she doesn't understand how AJAX works. – lonesomeday Oct 12 '12 at 17:25
  • who cares about convetions and "how stuff works in smallest detail"? :) "Good artists copy, great artists steal." :) –  Oct 12 '12 at 17:26
0

The issue is that this is an asynchronous call. alert is trying to show the result of $.inlDo, but it doesn't return the function you specified as a callback.

The $.inlDo function returns without blocking, i.e. it doesn't wait for the ajax call to complete and then continue executing. The ajax call can complete after (even a while after) $.inlDo returns.

Then what happens is that the callback success gets executed once the data is returned from the server (i.e. the ajax call completes). In this callback you have to deal with the result. This function is the one that should do the alert (like @Zlatan suggests) or update the webpage, etc.

If you want the call to block, i.e. to wait until the response returns, then use the async: false configuration. There are some limitations on when you can use synchronous (blocking) calls.

More info: http://api.jquery.com/jQuery.ajax/

frozenkoi
  • 3,228
  • 22
  • 33