6

やあみんな!

次のajax()ような電話があります。

$.ajax({
  type: "post",
  url: "whatever.php",
  data: {
    theData: "moo moo"
  },
  success: function(data) {
    console.log(data);
  }
});

これをカスタム関数内にラップして、コールバックを保持することは可能ですか?
何かのようなもの:

function customAjax(u, d, theCallbackStuff) {
  $.ajax({
    type: "post",
    url: u,
    data: d,
    success: function(data) {
      //RUN theCallbackStuff
    }
  });
}

theCallbackStuff次のようになります。

var m = 1;
var n = 2;
alert(m + n + data);
4

3 に答える 3

11

編集:

最近これに賛成票を投じたので、もうこのやり方はしないと言わざるを得ません。$.ajaxを返すpromiseので、Promise を直接使用して、ここで行ったことのほとんどをより一貫性のある堅牢な方法で行うことができます。

function customRequest(u,d) {
   var promise = $.ajax({
     type: 'post',
     data: d,
     url: u
   })
   .done(function (responseData, status, xhr) {
       // preconfigured logic for success
   })
   .fail(function (xhr, status, err) {
      //predetermined logic for unsuccessful request
   });

   return promise;
}

次に、使用法は次のようになります。

// using `done` which will add the callback to the stack 
// to be run when the promise is resolved
customRequest('whatever.php', {'somekey': 'somevalue'}).done(function (data) {
   var n = 1,
       m = 2;

   alert(m + n + data);
});

// using fail which will add the callback to the stack 
// to be run when the promise is rejected
customRequest('whatever.php', {'somekey': 'somevalue'}).fail(function (xhr, status, err) {
   console.log(status, err);
});

// using then which will add callabcks to the 
// success AND failure stacks respectively when 
// the request is resolved/rejected
customRequest('whatever.php', {'somekey': 'somevalue'}).then(
  function (data) {
     var n = 1,
         m = 2;

     alert(m + n + data);
  },
  function (xhr, status, err) {
     console.log(status, err);
  });

確かに私はこれをいつもやっています。実際の成功コールバック内でコールバックを実行するか、コールバックを成功コールバックとして割り当てることができます。

function customRequest(u,d,callback) {
   $.ajax({
     type: "post",
     url: u,
     data:d,
     success: function(data) {
        console.log(data); // predefined logic if any

        if(typeof callback == 'function') {
           callback(data);
        }
     }
  });
}

使用法は次のようになります。

customRequest('whatever.php', {'somekey': 'somevalue'}, function (data) {
   var n = 1,
       m = 2;

   alert(m + n + data);
});
于 2012-10-10T14:05:45.853 に答える
5
    function customAjax(u, d, theCallbackStuff) {
      $.ajax({
        type: "post",
        url: u,
        data: d,
        success: theCallbackStuff 
      });
    }

customAjax(url, data, function(data){
//do something
});
于 2012-10-10T14:01:33.573 に答える
1

このメモでは、完全な関数をコールバックとしてこれに渡すことができます。

function customRequest(u,d,callback) {
   $.ajax({
     type: "post",
     url: u,
     data:d,
     success: function(data) {
        console.log(data); // predefined logic if any

        if(typeof callback == 'function') {
           callback(data);
        }
     }
  });
}



// Then call it as follows:

function initiator() {

    customRequest( '/url/to/post', 'param1=val', function() { alert( 'complete' ); })

}

匿名関数として渡すだけでも機能します..表示するためだけに:)

于 2012-10-10T14:17:03.127 に答える