开发者

javascript: returning a bool

I have written a jQuery / JS function which "runs a php query".

function runQuery(op){
if(op.type == "edit"){
    var b = false;
    if(op.id != "" && (op.fromSong || op.toSong || op.when || op.comment)){
    $.post("processor.php", { id: op.id, type: "edit", fromSong: op.fromSong, toSong: op.toSong, when: op.when, comment: op.comment }, function(data){
        if(data == true){
            console.log(true);
            b = true;
        }else{
            console.log(false);
             b = false;
        }
    });
    }
    return b;
}

I want it to return true of false depending on what the server answers. I'm sure that the php script is working correctly and 开发者_如何学Creturning true or false correctly. But every time i run the function the console.log() outputs the correct value, unlike the variable b. It seems to alway be false. What am I doing wrong?


Since any .ajax() call ($.post is just a wrapper to $.ajax) runs asyncronously, your variable b will always return false. Best thing to "workaround" this is to pass in another callback:

function runQuery(op, callback){
    if(op.type == "edit"){
        if(op.id != "" && (op.fromSong || op.toSong || op.when || op.comment)){
            $.post("processor.php", { id: op.id, type: "edit", fromSong: op.fromSong, toSong: op.toSong, when: op.when, comment: op.comment }, function(data){
        if(data == true){
            console.log(true);
            callback.apply(this, [data]);
        }else{
            console.log(false);
            callback.apply(this, [data]);
        }
     });
    }
}

runQuery({
    type: 'edit',
}, function(data) {
   alert(data);
});


yes it is wrong.

$.post is done asynchronously, so b is set in the call back but returned before you reach the callback.

you should use $.ajax instead with method:"POST", and async: false

However it could be better to have a deisgn when you use the value of b in the callback only because async: false can freeze your browser.


Your function will return before the asynchronous request to the server has completed. Before you ask, it is both impractical and undesierable to make the call synchronous so that you can return true/false from your function. You might consider supplying a callback argument or arguments to your function:

function runQuery(op, success, failure){
   // ...
        if(data == true){
          success();
        }else{
          failure();
        }
   // ...
}

runQuery('edit',
  function () { alert('success!'); },
  function () { alert('failure!'); }
);


This is a wrong way to do it. Ajax requests are asynchronous and not performed one after another as you expect them too. There is no way around that. You will have to redesign your code to nest ajax calls.

Something similar to this:

$.post("a.php", {function(data){
    //1st check 
    $.post("b.php", {function(data){
        //2nd check 
        $.post("c.php", {function(data){
            //final actions
        });  
    });    
});

Only this way ajax calls will be stacked - the next will be performed after the previous.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜