将jquery ajax请求设置为async = false不起作用

时间:2022-10-07 14:36:31

I'm attempting to get started with google wallet and am generating a jwt token via an ajax request.

我正试图开始使用谷歌钱包,并通过ajax请求生成一个jwt令牌。

When a user hits the purchase button it fires the purchase() function which in turn sends off some data to get the jwt using the get_jwt_token_for_user() function. I've set the ajax request to not be asynchronous to ensure that the jwt is sent to the google payments handler.

当用户点击购买按钮时,它会触发purchase()函数,该函数又发送一些数据以使用get_jwt_token_for_user()函数获取jwt。我已经将ajax请求设置为不是异步的,以确保将jwt发送到google payment handler。

However the purchase() function seems to continue before the jwt is returned by the get_jwt_token_for_user() function. The log output shows that the numbers 1 and 2 are printed to console before the jwt is printed to the console from the get_jwt_token_for_user() function.

但是,在get_jwt_token_for_user()函数返回jwt之前,purchase()函数似乎仍在继续。日志输出显示在从get_jwt_token_for_user()函数将jwt打印到控制台之前,数字1和2将打印到控制台。

function get_jwt_token_for_user(the_key)
{
    var JwtTokenURL = "/get_jwt_token";
    var the_user_name = $('#user_name').val();
    var the_user_email = $('#user_email').val();
    var the_user_number = $('#user_number').val();
    $.ajax({ 
        type: "Get",
        url: JwtTokenURL,
        data: {user_number : the_user_number, user_name : the_user_name, user_email : the_user_email, the_d_key : the_key},
        async: false,
        success: function(result) {
            var myObject = JSON.parse(result);
            console.log(myObject.jwt_token);
            return myObject.jwt_token
        },
        failure: function(fail){ alert(fail); }
     });
}

function purchase(the_key)
{
    console.log("1");
    var jwt_token = get_jwt_token_for_user(the_key);
    console.log("2");
    if (jwt_token !== "")
    {
        console.log(jwt_token);
        goog.payments.inapp.buy({
            parameters: {},
            'jwt'     : jwt_token,
            'success' : successHandler,
            'failure' : failureHandler
          });
    }
}

Any idea what I can do to ensure that the ajax request has returned the data before the purchase() function marches on without the jwt value?

知道我可以做些什么来确保ajax请求在purchase()函数没有jwt值之前返回数据?

1 个解决方案

#1


5  

Your get_jwt_token_for_user function doesn't return anything, you need something more like this:

你的get_jwt_token_for_user函数没有返回任何内容,你需要更像这样的东西:

function get_jwt_token_for_user(the_key) {
    //...
    var myObject;
    $.ajax({ 
        //...
        success: function(result) {
            myObject = JSON.parse(result);
        },
        //...
     });
     return myObject ? myObject.jwt_token : '';
}

Returning something from your success callback doesn't cause that value to be returned by $.ajax and JavaScript functions do not return the value of their last expressions, you must include an explicit return if you want your function to return something.

从成功回调中返回一些内容不会导致$ .ajax返回该值,并且JavaScript函数不返回其最后一个表达式的值,如果希望函数返回某些内容,则必须包含显式返回。

You should also stop using async:false as soon as possible, it is rather user-hostile and it is going away. Your code should look more like this:

你也应该尽快停止使用async:false,它是用户敌对的,它会消失。您的代码看起来应该更像这样:

function get_jwt_token_for_user(the_key, callback) {
    //...
    $.ajax({ 
        type: "Get",
        url: JwtTokenURL,
        data: {user_number : the_user_number, user_name : the_user_name, user_email : the_user_email, the_d_key : the_key},
        success: function(result) {
            var myObject = JSON.parse(result);
            callback(myObject.jwt_token);
        },
        failure: function(fail){ alert(fail); }
     });
}

function purchase(the_key) {
    get_jwt_token_for_user(the_key, function(jwt_token) {
        if (jwt_token !== "") {
            //...
        }
    });
}

#1


5  

Your get_jwt_token_for_user function doesn't return anything, you need something more like this:

你的get_jwt_token_for_user函数没有返回任何内容,你需要更像这样的东西:

function get_jwt_token_for_user(the_key) {
    //...
    var myObject;
    $.ajax({ 
        //...
        success: function(result) {
            myObject = JSON.parse(result);
        },
        //...
     });
     return myObject ? myObject.jwt_token : '';
}

Returning something from your success callback doesn't cause that value to be returned by $.ajax and JavaScript functions do not return the value of their last expressions, you must include an explicit return if you want your function to return something.

从成功回调中返回一些内容不会导致$ .ajax返回该值,并且JavaScript函数不返回其最后一个表达式的值,如果希望函数返回某些内容,则必须包含显式返回。

You should also stop using async:false as soon as possible, it is rather user-hostile and it is going away. Your code should look more like this:

你也应该尽快停止使用async:false,它是用户敌对的,它会消失。您的代码看起来应该更像这样:

function get_jwt_token_for_user(the_key, callback) {
    //...
    $.ajax({ 
        type: "Get",
        url: JwtTokenURL,
        data: {user_number : the_user_number, user_name : the_user_name, user_email : the_user_email, the_d_key : the_key},
        success: function(result) {
            var myObject = JSON.parse(result);
            callback(myObject.jwt_token);
        },
        failure: function(fail){ alert(fail); }
     });
}

function purchase(the_key) {
    get_jwt_token_for_user(the_key, function(jwt_token) {
        if (jwt_token !== "") {
            //...
        }
    });
}