Skip to content

Instantly share code, notes, and snippets.

@weirongxu
Last active August 29, 2015 14:25
Show Gist options
  • Save weirongxu/b4e9f4819cfa88be1a67 to your computer and use it in GitHub Desktop.
Save weirongxu/b4e9f4819cfa88be1a67 to your computer and use it in GitHub Desktop.
;(function($) {
$.fn.extend({
ajaxForm: function(){
var action = this.attr('action');
var method = this.attr('method');
var d = $.Deferred();
this.on('submit', function(){
fetch(action, {
method: method,
credentials: 'same-origin',
body: new FormData(this[0]),
}).then(function(data){
d.resolve(data);
}).catch(function(data){
d.reject(data);
});
return false;
});
return d.promise();
},
});
})(jQuery);
@weirongxu
Copy link
Author

$('form').ajaxForm()
.then(function(res){
  return res.json();
}).then(function(json){
  // handle json
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment