0
function addrewarddb()
{
    var rval="98";
    $.post("db/mkreward.php",{
        proid:getURLParameter("id")
    },function(data){


         rval="99";
         alert(rval);
    });
    alert(rval);
     return rval;

}

when this function executes the function returns 98 instead of 99.the two alerts showing 98,then 99.how to make my function returns 99.i think the problem here is the function returnig before the response from the server page mkreward.php.how i can make the function return after the response from server

5 Answers 5

4

The $.post call is asynchronous.

This means that the function returns before the HTTP request has been completed.

Then, when the $.post() call finishes, it then updates the variable and alerts 99.

From the jquery docs:

async Boolean Default: true By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the complete/success/error callbacks.

Sign up to request clarification or add additional context in comments.

Comments

2

Put the return into the callback function:

function addrewarddb()
{
    var rval="98";
    $.post("db/mkreward.php",{
        proid:getURLParameter("id")
    },function(data){


         rval="99";
         alert(rval);
         return rval;
    });


}

Comments

2

You have async of post call and your return statement will execute before your callback function is called. You can return the post object from addrewarddb() and call complete on it like this.

function addrewarddb()
{
    var rval="98";
    return $.post("db/mkreward.php",{
        proid:getURLParameter("id")
    })
}

addrewarddb().complete (function (data){
       alert( data);
       return data;
});

Comments

1

You can make a synchronous jquery request using

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  async:false
});

or wait for the callback function to get the result

Comments

1

This is because $.post works asynchronously. $post is just a shorthand for $.ajax. To be able to tell $.post to work synchronously (which is what you want) you have to use $.ajax.

Your function's translation should be something like this:

function addrewarddb() {
    var rval = "98";

    $.ajax({
        type: "POST",
        url: "db/mkreward.php",
        data: { proid: getURLParameter("id") },
        async: false,
        success: function (msg) {
            rval = "99";
            alert(rval);
        },
        error: function (jqXHR, textStatus) {

        }
    });

    alert(rval);
    return rval;
}

Check out jquery site for details.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.