将同步XMLHttpRequest转换为异步

时间:2018-07-31 00:47:18

标签: javascript xmlhttprequest

我写的东西目前正在工作,但是我认为我写的JS不够,没有正确的心态来解决这个问题。

这是签名表单提交的一小段代码。表单中嵌入了一个随机数,该随机数用于通过XHR检索短暂的秘密值,然后使用XHR计算表单内容的HMAC。

我编写的代码基本上是:

getSecret: function(nonce) {
    xhttp = new XMLHttpRequest();
    xhttp.open("GET", ajax_url + "?nonce=" + nonce, false);
    xhttp.send();
    if(xhttp.status != 200) {
        console.log('Nonce/secret exchange failed')
        return ""
    } else {
        return JSON.parse(xhttp.responseText)
    }
},

formSubmit: function(name) {
    // pull out the nonce
    nonce = 'noncey_mcnonceface'
    secret = getSecret(nonce)
    // sign and send the request
}

回调此方法的正确方法是什么?

0 个答案:

没有答案
相关问题