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.

3 Answers 3

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");
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I think this has nothing to do with the problem guy just asked.
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

7 Comments

This was helpful. For me, the "ok" value had to go into editFile. Cheers
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.
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.
@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.
@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)! :)
|
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/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.