From c95701e1ee8f0fb2fe5f7a7a3ea9bfc30e0989bf Mon Sep 17 00:00:00 2001 From: Zete Lui Date: Tue, 10 Mar 2015 14:43:17 +0800 Subject: [PATCH 1/2] Also serialize file fields in the form --- README.md | 4 +++- app/assets/javascripts/api_taster/app.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b1ca8c..c4c1d4b 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,9 @@ if Rails.env.development? post '/users', { :user => { - :name => 'Fred' + :name => 'Fred', + # a file field + :avatar => :file } } diff --git a/app/assets/javascripts/api_taster/app.js b/app/assets/javascripts/api_taster/app.js index 292309f..196077c 100644 --- a/app/assets/javascripts/api_taster/app.js +++ b/app/assets/javascripts/api_taster/app.js @@ -131,7 +131,9 @@ jQuery(function($) { }, url: ApiTaster.getSubmitUrl($form), type: $form.attr('method'), - data: $form.serialize() + data: new FormData(e.target), + processData: false, + contentType: false }).complete(onComplete); ApiTaster.lastRequest = {}; From dd21551fccfddea5fa9367abe19781d4a72f7517 Mon Sep 17 00:00:00 2001 From: Zete Lui Date: Fri, 20 Mar 2015 16:45:18 +0800 Subject: [PATCH 2/2] Stop using FormData for GET/HEAD requests jQuery doesn't serialize it when doing ajax --- README.md | 2 +- app/assets/javascripts/api_taster/app.js | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c4c1d4b..c11d0db 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ if Rails.env.development? post '/users', { :user => { :name => 'Fred', - # a file field + # a file field, it doesn't have effect in GET request :avatar => :file } } diff --git a/app/assets/javascripts/api_taster/app.js b/app/assets/javascripts/api_taster/app.js index 196077c..dc03840 100644 --- a/app/assets/javascripts/api_taster/app.js +++ b/app/assets/javascripts/api_taster/app.js @@ -122,7 +122,7 @@ jQuery(function($) { ApiTaster.disableSubmitButton(); ApiTaster.disableUrlParams(); - window.ajax = $.ajax({ + var ajaxParams = { beforeSend: function(xhr) { var headers = ApiTaster.headers; for(var l = headers.length, i = 0; i < l; i ++) { @@ -130,11 +130,18 @@ jQuery(function($) { } }, url: ApiTaster.getSubmitUrl($form), - type: $form.attr('method'), - data: new FormData(e.target), - processData: false, - contentType: false - }).complete(onComplete); + type: $form.attr('method') + } + + if (!ajaxParams.type || ajaxParams.type.toLowerCase() === 'get' || ajaxParams.type.toLowerCase() === 'head') { + ajaxParams.data = $form.serialize() + } else { + ajaxParams.data = new FormData(e.target) + ajaxParams.processData = false + ajaxParams.contentType = false + } + + window.ajax = $.ajax(ajaxParams).complete(onComplete); ApiTaster.lastRequest = {}; ApiTaster.lastRequest.startTime = Date.now();