最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Making a JQuery ajax request conditional - Stack Overflow

programmeradmin1浏览0评论

I would like to skip all this piece of code if some preconditions are not met, but I also want to move all the code between the parenthesis in a function. Is it allowed? I don't understand how this syntax works.

    $.ajax({
        type: "POST",
        url: urlAppend,
        data: JSON.stringify(xxx),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: false,
        success: function (result) {
            if (canceled) {
                return;
            }
                //Long code
            }
        //Long code 2
        },
        error: function (request, error) {
            alert('ppp');
        }
    });

I would like to skip all this piece of code if some preconditions are not met, but I also want to move all the code between the parenthesis in a function. Is it allowed? I don't understand how this syntax works.

    $.ajax({
        type: "POST",
        url: urlAppend,
        data: JSON.stringify(xxx),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: false,
        success: function (result) {
            if (canceled) {
                return;
            }
                //Long code
            }
        //Long code 2
        },
        error: function (request, error) {
            alert('ppp');
        }
    });
Share Improve this question edited Oct 29, 2014 at 15:33 Revious asked Oct 29, 2014 at 15:28 ReviousRevious 8,16633 gold badges102 silver badges157 bronze badges 12
  • 2 yes you can.. did you even try ? – Prince Singh Commented Oct 29, 2014 at 15:29
  • You want to skip the entire call to $ajax? Can't you just wrap that code in an if statement? – Lix Commented Oct 29, 2014 at 15:29
  • @Lix: yes, I don't want ajax to be called. – Revious Commented Oct 29, 2014 at 15:31
  • 1 The same way you "mix the syntax" of writing a function and writing any other statement. – Quentin Commented Oct 29, 2014 at 15:35
  • 1 This is quite elementary, which is why you are getting a lot of confused looks right now. Step 1: Write a function. Step 2: Write an if statement inside the function. Step 3: Copy and paste the $.ajax code into the if statement. Step 4: Call the function to test it. What's the problem? – Brett Commented Oct 29, 2014 at 15:39
 |  Show 7 more ments

4 Answers 4

Reset to default 5

Place your $.ajax call into a function, then wrap the call to the function in a conditional expression:

function makeRequest(){
  $.ajax( ... )
}

if ( some_condition ){
  makeRequest();
}

Keep in mind that you are using some variables inside the AJAX callback (namely the canceled variable). You'll have to make that variable available to the function.

function doComplexStuff(){

} 

$.ajax({
        type: "POST",
        url: urlAppend,
        data: JSON.stringify(xxx),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: false,
        success: doComplexStuff,
        error: function (request, error) {
            alert('ppp');
        }
    });

The doComplexStuff will automatically receive all the params that the success function receives.

Take a look at this sample:

function callAjax(condition) {
    if (condition) {
        $.ajax({
            type: "POST",
            url: urlAppend,
            data: JSON.stringify(xxx),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processdata: false,
            success: function (result) {
                if (canceled) {
                    return;
                }
                    //Long code
                }
            //Long code 2
            },
            error: function (request, error) {
                alert('ppp');
            }
        });
    }
}

// Somewhere in your code
callAjax(condition);

Try this:

function runAjax(params){
    $.ajax({
        type: params["post"],
        url: params["url"],
        data: params["data"],
        contentType: params["content_type"],
        dataType: params["json"],
        processdata: params["process_bool"],
        success: function (result) {
            if (params["canceled"]) {
                return;
            }
                //Long code
            }
        //Long code 2
        },
        error: function (request, error) {
            alert('ppp');
        }
    });
}

if(condition){
  var options = {
      //set options here
  };
  runAjax(options);
}
发布评论

评论列表(0)

  1. 暂无评论