开发者

pass server response to call back function in jquery plugin

开发者 https://www.devze.com 2023-02-03 20:15 出处:网络
So basically, I have a small plugin like below, what it does is submit the form with jquery method post.

So basically, I have a small plugin like below, what it does is submit the form with jquery method post.

(function($){
 $.fn.ajaxSubmit = function(options){
  var defaults = {
   url:'',
   success:function(){},
   error:function(){}
  },
  o = $.extend({},defaults, options);
  return this.each(function(){
   var $this=$(this);
   $this.submit(function(e){
    $.post(o.url,$this.serialize() ,
    function(response){
     if(response.status == 'success'){
      o.success.call(this);
     }
     else if(response.status == 'error'){
      o.error.call(this);
     }
    },'json');
    return false;
   });
  });
 }
})(jQuery);

This is how its called

$('#form').ajaxSubmit({
  url:'www.myownwebsite.com/play/processform',
  success:function(response){alert(response.status)},
  error:function(response){alert(response.status);}
 });

The problem is, when the script is executed开发者_C百科, I get the error "response is undefined". Clearly I know whats wrong, but how can I do it properly?


have you tried o.success.call(this,response);

0

精彩评论

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