简体   繁体   中英

Regarding post requests

Say I have the code:

var testVar = 0;
var newVar = "";

function(){
    var info = "hello";
    $.post("test.php", {"info":info}, function(data){
        if(data == "success"){
             testVar = 1;
        }
        else{
             testVar = 0;
        }
    });
    $.post("new.php", {"testVar":testVar}, function(data2){
        if(data2 == "success"){
            newVar = "Complete";
        }
        else{
            newVar = "Failed";
        }
    });
}

Assumning test.php returns "success" and new.php needs aa 1 for testVar to return success, how do I get a "Complete" for newVar? I'm guessing that the second post request would happen before the first returns the data.

You can do:

var testVar = 0;
var newVar = "";

var secondFunction = function(){
     $.post("new.php", {"testVar":testVar}, function(data2){
        if(data2 == "success"){
            newVar = "Complete";
        }
        else{
            newVar = "Failed";
        }
    });
};
function(){
    var info = "hello";
    $.post("test.php", {"info":info}, function(data){
        if(data == "success"){
             testVar = 1;
        }
        else{
             testVar = 0;
        }
        secondFunction();
    });

}

If the parameters for the second request depend on the results coming from the first,
then make sure you send the requests in sequence,
meaning send the second post only after you have the response from the first.

Also you should prepare your responses to include a flag for success and another for the payload.

Successful operation

{success : "true", message : "operation successful", value : "1"}

Operation failed

{success : "false", message : "operation failed", value : "0"}

Consider the following example

function(){

    var info = "hello";

    $.post("test.php", {"info":info}, function(data){

        if (data.success != false){

            $.post("new.php", {"testVar":data.value}, function(data){
                if (data.success != false){
                    console.log(data.message) // this is the success message from the second request
                    // process the data from the second response,
                    // var = data.value ...
                }
                else{
                    console.log(data.message) // handle the failed state for the second request
                }
            },"json");

        }
        else{
            console.log(data.message)
        }

    },"json");

}

The second request will be fired only if the first one has been successful.
You have some consistency in your responses, the contents of value can be a single value, an array or an object.
Having values for success and a message, you can easily track what happened and if needed bring up notifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM