如何使用AJAX作为同步操作

时间:2019-12-09 06:07:15

标签: javascript asynchronous async-await

以下函数检查我的表单中的错误。必须在服务器中验证字段之一。但是,当我发布该字段时,该函数将继续并从该函数返回,然后再从服务器获取答案。从函数返回之前,我必须从服务器获取结果。 这是我的代码:

function checkErrors()
    {
        var ErrorMsg = "";          
        var buyerPublicKey = document.getElementById("BuyerAddressTxt").value;
        if (buyerPublicKey == "")
        {
            ErrorMsg = ErrorMsg+"Please Insert buyer Public Key.<br>";         
        } 

        var dealPrice = document.getElementById("PriceEthTxt").value;
        if (dealPrice == "")
        {
            ErrorMsg = ErrorMsg+"Please Insert buyer price.<br>";     
        }
        var timeTobeOpen = document.getElementById("TimeSelector").value;
        if (timeTobeOpen == "Deafult")
        {
            ErrorMsg = ErrorMsg + "Please Insert time.<br>";
        }
        var url = "/CreateContract/CheckBuyerPublicKeyLegality";
        $.post(url, { BuyerPublicKey: buyerPublicKey }, function (data) //Here the problem begins
        {
           console.log("Buyer balance"+data)
           if (data == -1)
           {
            ErrorMsg = ErrorMsg + "Buyer Public Key is illegal.<br>";//
            console.log("1-->" +ErrorMsg );
           }

         });
        console.log("2-->" +ErrorMsg );
        return ErrorMsg;
    }

如您所见,我在1-> ErrorMessage之前得到了2-> ErrorMessage,这意味着发布是异步操作的,而我需要它是同步的。

我可以在此处进行更改吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用.done()函数

function checkErrors()
{
    var ErrorMsg = "";          
    var buyerPublicKey = document.getElementById("BuyerAddressTxt").value;
    if (buyerPublicKey == "")
    {
        ErrorMsg = ErrorMsg+"Please Insert buyer Public Key.<br>";         
    } 

    var dealPrice = document.getElementById("PriceEthTxt").value;
    if (dealPrice == "")
    {
        ErrorMsg = ErrorMsg+"Please Insert buyer price.<br>";     
    }
    var timeTobeOpen = document.getElementById("TimeSelector").value;
    if (timeTobeOpen == "Deafult")
    {
        ErrorMsg = ErrorMsg + "Please Insert time.<br>";
    }
    var url = "/CreateContract/CheckBuyerPublicKeyLegality";
    $.post(url, { BuyerPublicKey: buyerPublicKey }, function (data) //Here the problem begins
    {
       console.log("Buyer balance"+data)
     }).done(function(data) {
       if (data == -1)
       {
        ErrorMsg = ErrorMsg + "Buyer Public Key is illegal.<br>";//
        console.log("1-->" +ErrorMsg );
       }
    });
    console.log("2-->" +ErrorMsg );
    return ErrorMsg;
}

答案 1 :(得分:0)

使用异步。默认情况下,异步设置为true,将其更改为false。像这样

function checkErrors(){
var ErrorMsg = "";          
var buyerPublicKey = document.getElementById("BuyerAddressTxt").value;
if (buyerPublicKey == "")
{
    ErrorMsg = ErrorMsg+"Please Insert buyer Public Key.<br>";         
} 

var dealPrice = document.getElementById("PriceEthTxt").value;
if (dealPrice == "")
{
    ErrorMsg = ErrorMsg+"Please Insert buyer price.<br>";     
}
var timeTobeOpen = document.getElementById("TimeSelector").value;
if (timeTobeOpen == "Deafult")
{
    ErrorMsg = ErrorMsg + "Please Insert time.<br>";
}
$.ajax({
    url: "/CreateContract/CheckBuyerPublicKeyLegality",
    type: 'POST',
    async: false,
    data : { BuyerPublicKey: buyerPublicKey },
    success: function(data){
        console.log("Buyer balance"+data)
        if (data == -1)
        {
            ErrorMsg = ErrorMsg + "Buyer Public Key is illegal.<br>";//
            console.log("1-->" +ErrorMsg );
        }
    }
});
console.log("2-->" +ErrorMsg );
return ErrorMsg;}