Replace rx_ajax iframe form submission with proper AJAX using FormData

This commit is contained in:
Kijin Sung 2024-10-09 17:21:38 +09:00
parent 206521a352
commit b598083a8c
2 changed files with 4 additions and 61 deletions

View file

@ -703,7 +703,7 @@ class ModuleHandler extends Handler
$procResult = $oModule->proc();
$methodList = array('XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1);
if(!$oModule->stop_proc && !isset($methodList[Context::getRequestMethod()]) && !isset($_SERVER['HTTP_X_AJAX_TARGET']) && !isset($_POST['_rx_ajax_form']))
if(!$oModule->stop_proc && !isset($methodList[Context::getRequestMethod()]))
{
$error = $oModule->getError();
$message = $oModule->getMessage();
@ -1014,28 +1014,6 @@ class ModuleHandler extends Handler
$methodList = array('XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1);
if(!isset($methodList[Context::getRequestMethod()]))
{
// Handle iframe form submissions.
$ajax_form_target = strval($_SERVER['HTTP_X_AJAX_TARGET'] ?? ($_POST['_rx_ajax_form'] ?? ''));
if($ajax_form_target !== '' && starts_with('_rx_temp_iframe_', $ajax_form_target))
{
$data = [];
if ($this->error)
{
$data['error'] = -1;
$data['message'] = lang($this->error);
}
else
{
$data['error'] = $oModule->error;
$data['message'] = lang($oModule->message);
}
$data = array_merge($data, $oModule->getVariables());
ob_end_clean();
echo sprintf('<html><head></head><body><script>parent.XE.handleIframeResponse(%s, %s);</script></body></html>', json_encode($ajax_form_target), json_encode($data));
return;
}
// Handle redirects.
if($oModule->getRedirectUrl())
{

View file

@ -216,11 +216,10 @@
var request_info;
if (action === 'raw') {
if (params instanceof FormData) {
request_info = params.get('module') + '.' + params.get('act');
request_info = (params.get('module') || params.get('mid')) + '.' + params.get('act');
} else {
request_info = 'RAW FORM SUBMISSION';
}
console.log(request_info);
} else {
params = params ? ($.isArray(params) ? arr2obj(params) : params) : {};
//if (action_parts.length != 2) return;
@ -385,7 +384,6 @@
* Function for AJAX submission of arbitrary forms.
*/
XE.ajaxForm = function(form, callback_success, callback_error) {
// Abort if the form already has a 'target' attribute.
form = $(form);
// Get success and error callback functions.
if (typeof callback_success === 'undefined') {
@ -411,43 +409,10 @@
callback_error = null;
}
}
// Set _rx_ajax_form flag
if (!form.find('input[name=_rx_ajax_form]').size()) {
form.append('<input type="hidden" name="_rx_ajax_form" value="json" />');
setTimeout(function() {
form.find('input[name=_rx_ajax_form]').remove();
}, 1000);
}
// If the form has file uploads, use a hidden iframe to submit. Otherwise use exec_json.
var has_files = form.find('input[type=file][name!=Filedata]').size();
if (has_files) {
var iframe_id = '_rx_temp_iframe_' + (new Date()).getTime();
$('<iframe id="' + iframe_id + '" name="' + iframe_id + '" style="display:none"></iframe>').appendTo($(document.body));
form.attr('method', 'POST').attr('enctype', 'multipart/form-data').attr('target', iframe_id);
form.find('input[name=_rx_ajax_form]').val(iframe_id);
window.XE.handleIframeResponse = function(iframe_id, data) {
if (data.error) {
if (callback_error) {
callback_error(data);
} else {
alert(data.message);
}
} else {
callback_success(data);
}
if (iframe_id.match(/^_rx_temp_iframe_[0-9]+$/)) {
$('iframe#' + iframe_id).remove();
}
};
setTimeout(function() {
form.removeAttr('target');
}, 1000);
form.submit();
} else {
window.exec_json('raw', form.serialize(), callback_success, callback_error);
}
window.exec_json('raw', new FormData(form[0]), callback_success, callback_error);
};
$(document).on('submit', 'form.rx_ajax', function(event) {
// Abort if the form already has a 'target' attribute.
if (!$(this).attr('target')) {
event.preventDefault();
XE.ajaxForm(this);