mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-05 17:51:40 +09:00
Submit raw JSON in Rhymix.ajax()
This commit is contained in:
parent
a339d88a2e
commit
59bb9c1043
1 changed files with 34 additions and 24 deletions
|
|
@ -403,11 +403,18 @@ Rhymix.modal.close = function(id) {
|
||||||
*/
|
*/
|
||||||
Rhymix.ajax = function(action, params, success, error) {
|
Rhymix.ajax = function(action, params, success, error) {
|
||||||
|
|
||||||
// Extract action info
|
// Extract module and act
|
||||||
|
let isFormData = params instanceof FormData;
|
||||||
|
let module, act;
|
||||||
if (!action) {
|
if (!action) {
|
||||||
if (params instanceof FormData) {
|
if (isFormData) {
|
||||||
action = (params.get('module') || params.get('mid')) + '.' + params.get('act');
|
module = params.get('module');
|
||||||
if (action === '.') {
|
act = params.get('act');
|
||||||
|
if (module && act) {
|
||||||
|
action = module + '.' + act;
|
||||||
|
} else if (act) {
|
||||||
|
action = act;
|
||||||
|
} else {
|
||||||
action = null;
|
action = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -416,13 +423,16 @@ Rhymix.ajax = function(action, params, success, error) {
|
||||||
} else {
|
} else {
|
||||||
action = action.split('.');
|
action = action.split('.');
|
||||||
params = params || {};
|
params = params || {};
|
||||||
params.module = action[0];
|
params.module = module = action[0];
|
||||||
params.act = action[1];
|
params.act = act = action[1];
|
||||||
action = action.join('.');
|
action = action.join('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add action to URL if the current rewrite level supports it
|
// Add action to URL if the current rewrite level supports it
|
||||||
let url = this.URI(window.request_uri).pathname();
|
let url = this.URI(window.request_uri).pathname() + 'index.php';
|
||||||
|
if (act) {
|
||||||
|
url = url + '?act=' + act;
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
if (this.getRewriteLevel() >= 2 && action !== null) {
|
if (this.getRewriteLevel() >= 2 && action !== null) {
|
||||||
url = url + action.replace('.', '/');
|
url = url + action.replace('.', '/');
|
||||||
|
|
@ -437,25 +447,25 @@ Rhymix.ajax = function(action, params, success, error) {
|
||||||
headers['X-CSRF-Token'] = getCSRFToken();
|
headers['X-CSRF-Token'] = getCSRFToken();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Generate AJAX parameters
|
||||||
|
const args = {
|
||||||
|
type: 'POST',
|
||||||
|
dataType: 'json',
|
||||||
|
url: url,
|
||||||
|
data: isFormData ? params : JSON.stringify(params),
|
||||||
|
contentType: isFormData ? false : 'application/json; charset=UTF-8',
|
||||||
|
processData: false,
|
||||||
|
headers: headers,
|
||||||
|
success: function(data, textStatus, xhr) {
|
||||||
|
Rhymix.ajaxSuccessHandler(xhr, textStatus, action, data, params, success, error);
|
||||||
|
},
|
||||||
|
error: function(xhr, textStatus, errorThrown) {
|
||||||
|
Rhymix.ajaxErrorHandler(xhr, textStatus, action, url, params, success, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Send the AJAX request
|
// Send the AJAX request
|
||||||
try {
|
try {
|
||||||
const args = {
|
|
||||||
type: 'POST',
|
|
||||||
dataType: 'json',
|
|
||||||
url: url,
|
|
||||||
data: params,
|
|
||||||
processData: (action !== null && !(params instanceof FormData)),
|
|
||||||
headers : headers,
|
|
||||||
success : function(data, textStatus, xhr) {
|
|
||||||
Rhymix.ajaxSuccessHandler(xhr, textStatus, action, data, params, success, error);
|
|
||||||
},
|
|
||||||
error : function(xhr, textStatus, errorThrown) {
|
|
||||||
Rhymix.ajaxErrorHandler(xhr, textStatus, action, url, params, success, error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (params instanceof FormData) {
|
|
||||||
args.contentType = false;
|
|
||||||
}
|
|
||||||
$.ajax(args);
|
$.ajax(args);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
alert(e);
|
alert(e);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue