有没有办法运行在ajaxSetup中定义的第一个基本函数?

时间:2010-07-29 08:12:37

标签: jquery ajax

$.ajaxSetup({
    success: function onSuccess(msg) {
        // add some functions to `msg` 
        // then return to success method that defined in $.ajax 
        msg.display = function(){
            alert(msg.M_Prop);
        }

        return msg;
    }
});

$.ajax({
    success: function(newMsg){
        // call new functions of newMsg object
        newMsg.display();
    }
});

1 个答案:

答案 0 :(得分:0)

这个怎么样?

$.ajaxSetup({
  url: "http://jsfiddle.net",
  global: false,
  type: "GET",
  display: function(msg) { // custom display function defined in ajax setup
    alert(msg);
  }
});

$.ajax({
  success: function(newMsg){
     this.display("Hello: " + newMsg); //call it in your success handler
  }
});

我在jsfiddle

上对此进行了测试

这是另一种方式(你想要的方式)

    $.ajaxSetup({
       url: "http://jsfiddle.net",
       global: false,
       type: "GET",
       success: function(msg) {
           msg = msg || {};
           msg.display = function() {
             alert("display");
           }
           if(typeof(this.customSuccess) === "function") { 
              this.customSuccess(msg); // call the custom success function
           }
       }
     });

    $.ajax({
        customSuccess: function(newMsg){ // define custom success function
            newMsg.display();
        }
    });

测试了此here