简体   繁体   English

您可以定义AJAX请求而无需在jQuery中发出请求吗?

[英]Can you define an AJAX request without making the request in jQuery?

With jQuery you can make an ajax request like this: 使用jQuery,您可以发出这样的ajax请求:

request=$.ajax({
  url: "test.html",
});

request.success(function(){
    alert('Yay!');
});

However, let's say I didn't want immediately make the ajax request. 但是,假设我不想立即发出ajax请求。 I just wanted to define it and bind it to an event. 我只是想定义它并将其绑定到事件。 Is there a way to do that? 有没有办法做到这一点?

$("#but").click(function() {
  $.ajax({
    url: "test.html",
    success: function(){
      //...
    }
  });
});

Would this suit your need? 这适合您的需求吗?

function callMeLater() {
    $.ajax({
        url: "test.html",
    });
}

//and later

$(selector).anyevent(function() {
    callMeLater();
});

Not really sure why you would do this but you could do something like this: 不太确定为什么要这样做,但是可以这样做:

var ajaxParams = {
   cache: false,
   success: function(){...
}

$("something").click(function(){
    $.ajax(ajaxParams);
});

Yes, sure. 是的,当然。

$(document).ready(function() {
  $('#somebutton').bind('click',function(){
    $.ajax({
      url: "test.html"
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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