mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-09 12:02:24 +09:00
Make all AJAX requests use regular form submission format and JSON response format
This commit is contained in:
parent
a0c22efe3f
commit
8ee5e8c4b0
2 changed files with 40 additions and 152 deletions
|
|
@ -214,8 +214,8 @@ class Context
|
||||||
{
|
{
|
||||||
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents("php://input");
|
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents("php://input");
|
||||||
|
|
||||||
// If content is not XML JSON, unset
|
// If content is not XML or JSON, unset
|
||||||
if(!preg_match('/^[\<\{\[]/', $GLOBALS['HTTP_RAW_POST_DATA']) && strpos($_SERVER['CONTENT_TYPE'], 'json') === FALSE && strpos($_SERVER['HTTP_CONTENT_TYPE'], 'json') === FALSE)
|
if(!preg_match('/^[\<\{\[]/', $GLOBALS['HTTP_RAW_POST_DATA']))
|
||||||
{
|
{
|
||||||
unset($GLOBALS['HTTP_RAW_POST_DATA']);
|
unset($GLOBALS['HTTP_RAW_POST_DATA']);
|
||||||
}
|
}
|
||||||
|
|
@ -232,7 +232,6 @@ class Context
|
||||||
$this->setRequestMethod('');
|
$this->setRequestMethod('');
|
||||||
|
|
||||||
$this->_setXmlRpcArgument();
|
$this->_setXmlRpcArgument();
|
||||||
$this->_setJSONRequestArgument();
|
|
||||||
$this->_setRequestArgument();
|
$this->_setRequestArgument();
|
||||||
$this->_setUploadedArgument();
|
$this->_setUploadedArgument();
|
||||||
|
|
||||||
|
|
@ -1276,7 +1275,7 @@ class Context
|
||||||
{
|
{
|
||||||
self::$_instance->request_method = $type;
|
self::$_instance->request_method = $type;
|
||||||
}
|
}
|
||||||
elseif (strpos($_SERVER['CONTENT_TYPE'], 'json') !== false || strpos($_SERVER['HTTP_CONTENT_TYPE'], 'json') !== false)
|
elseif (strpos($_SERVER['HTTP_ACCEPT'], 'json') !== false || strpos($_SERVER['CONTENT_TYPE'], 'json') !== false || strpos($_SERVER['HTTP_CONTENT_TYPE'], 'json') !== false)
|
||||||
{
|
{
|
||||||
self::$_instance->request_method = 'JSON';
|
self::$_instance->request_method = 'JSON';
|
||||||
}
|
}
|
||||||
|
|
@ -1336,7 +1335,7 @@ class Context
|
||||||
{
|
{
|
||||||
$set_to_vars = TRUE;
|
$set_to_vars = TRUE;
|
||||||
}
|
}
|
||||||
elseif($requestMethod == 'POST' && isset($_POST[$key]))
|
elseif(($requestMethod == 'POST' || $requestMethod == 'JSON') && isset($_POST[$key]))
|
||||||
{
|
{
|
||||||
$set_to_vars = TRUE;
|
$set_to_vars = TRUE;
|
||||||
}
|
}
|
||||||
|
|
@ -1387,18 +1386,7 @@ class Context
|
||||||
*/
|
*/
|
||||||
private function _setJSONRequestArgument()
|
private function _setJSONRequestArgument()
|
||||||
{
|
{
|
||||||
if(self::getRequestMethod() != 'JSON')
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$params = array();
|
|
||||||
parse_str($GLOBALS['HTTP_RAW_POST_DATA'], $params);
|
|
||||||
|
|
||||||
foreach($params as $key => $val)
|
|
||||||
{
|
|
||||||
self::set($key, $this->_filterRequestVar($key, $val, 1), TRUE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -201,25 +201,21 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
* @author NAVER (developers@xpressengine.com)
|
* @author NAVER (developers@xpressengine.com)
|
||||||
**/
|
**/
|
||||||
$.exec_xml = window.exec_xml = function(module, act, params, callback_func, response_tags, callback_func_arg, fo_obj) {
|
$.exec_xml = window.exec_xml = function(module, act, params, callback_func, response_tags, callback_func_arg, fo_obj) {
|
||||||
var xml_path = request_uri+"index.php";
|
params = params ? ($.isArray(params) ? arr2obj(params) : params) : {};
|
||||||
if(!params) params = {};
|
|
||||||
|
|
||||||
// {{{ set parameters
|
|
||||||
if($.isArray(params)) params = arr2obj(params);
|
|
||||||
params.module = module;
|
params.module = module;
|
||||||
params.act = act;
|
params.act = act;
|
||||||
|
|
||||||
if(typeof(xeVid)!='undefined') params.vid = xeVid;
|
if(typeof(xeVid) != "undefined") params.vid = xeVid;
|
||||||
if(typeof(response_tags) == "undefined" || response_tags.length<1) {
|
if(typeof(response_tags) == "undefined" || response_tags.length < 1) {
|
||||||
response_tags = ['error','message'];
|
response_tags = ['error','message'];
|
||||||
} else {
|
} else {
|
||||||
response_tags.push('error', 'message');
|
response_tags.push('error', 'message');
|
||||||
}
|
}
|
||||||
// }}} set parameters
|
|
||||||
|
|
||||||
// use ssl?
|
// use ssl?
|
||||||
|
var url = request_uri;
|
||||||
if ($.isArray(ssl_actions) && params.act && $.inArray(params.act, ssl_actions) >= 0) {
|
if ($.isArray(ssl_actions) && params.act && $.inArray(params.act, ssl_actions) >= 0) {
|
||||||
var url = default_url || request_uri;
|
url = default_url || request_uri;
|
||||||
var port = window.https_port || 443;
|
var port = window.https_port || 443;
|
||||||
var _ul = $('<a>').attr('href', url)[0];
|
var _ul = $('<a>').attr('href', url)[0];
|
||||||
var target = 'https://' + _ul.hostname.replace(/:\d+$/, '');
|
var target = 'https://' + _ul.hostname.replace(/:\d+$/, '');
|
||||||
|
|
@ -228,77 +224,28 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
if(_ul.pathname[0] != '/') target += '/';
|
if(_ul.pathname[0] != '/') target += '/';
|
||||||
|
|
||||||
target += _ul.pathname;
|
target += _ul.pathname;
|
||||||
xml_path = target.replace(/\/$/, '')+'/index.php';
|
url = target.replace(/\/$/, '')+'/';
|
||||||
}
|
}
|
||||||
|
|
||||||
var _u1 = $('<a>').attr('href', location.href)[0];
|
var _u1 = $('<a>').attr('href', location.href)[0];
|
||||||
var _u2 = $('<a>').attr('href', xml_path)[0];
|
var _u2 = $('<a>').attr('href', url)[0];
|
||||||
|
|
||||||
// 현 url과 ajax call 대상 url의 schema 또는 port가 다르면 직접 form 전송
|
// 현 url과 ajax call 대상 url의 schema 또는 port가 다르면 직접 form 전송
|
||||||
if(_u1.protocol != _u2.protocol || _u1.port != _u2.port) return send_by_form(xml_path, params);
|
if(_u1.protocol != _u2.protocol || _u1.port != _u2.port) return send_by_form(url, params);
|
||||||
|
|
||||||
var xml = [],
|
|
||||||
xmlHelper = function(params) {
|
|
||||||
var stack = [];
|
|
||||||
|
|
||||||
if ($.isArray(params)) {
|
|
||||||
$.each(params, function(key, val) {
|
|
||||||
stack.push('<value type="array">' + xmlHelper(val) + '</value>');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if ($.isPlainObject(params)) {
|
|
||||||
$.each(params, function(key, val) {
|
|
||||||
stack.push('<' + key + '>' + xmlHelper(val) + '</' + key + '>');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (!$.isFunction(params)) {
|
|
||||||
stack.push('<![CDATA[' + params + ']]>');
|
|
||||||
}
|
|
||||||
|
|
||||||
return stack.join('\n');
|
|
||||||
};
|
|
||||||
|
|
||||||
xml.push('<?xml version="1.0" encoding="utf-8" ?>');
|
|
||||||
xml.push('<methodCall>');
|
|
||||||
xml.push('<params>');
|
|
||||||
xml.push(xmlHelper(params));
|
|
||||||
xml.push('</params>');
|
|
||||||
xml.push('</methodCall>');
|
|
||||||
|
|
||||||
var _xhr = null;
|
var _xhr = null;
|
||||||
if (_xhr && _xhr.readyState !== 0) _xhr.abort();
|
if (_xhr && _xhr.readyState !== 0) _xhr.abort();
|
||||||
|
|
||||||
// 전송 성공시
|
// 전송 성공시
|
||||||
function onsuccess(data, textStatus, xhr) {
|
function onsuccess(data, textStatus, xhr) {
|
||||||
var resp_xml = $(data).find('response')[0], resp_obj, txt='', ret=[], tags={}, json_str='';
|
|
||||||
|
|
||||||
waiting_obj.css('display', 'none').trigger('cancel_confirm');
|
waiting_obj.css('display', 'none').trigger('cancel_confirm');
|
||||||
|
var ret = [];
|
||||||
if(!resp_xml) {
|
var tags = {};
|
||||||
alert(_xhr.responseText);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
json_str = xml2json(resp_xml, false, false);
|
|
||||||
resp_obj = jQuery.parseJSON(json_str);
|
|
||||||
resp_obj = resp_obj.response;
|
|
||||||
|
|
||||||
if (typeof(resp_obj)=='undefined') {
|
|
||||||
ret.error = -1;
|
|
||||||
ret.message = 'Unexpected error occured.';
|
|
||||||
try {
|
|
||||||
if(typeof(txt=resp_xml.childNodes[0].firstChild.data)!='undefined') {
|
|
||||||
ret.message += '\r\n'+txt;
|
|
||||||
}
|
|
||||||
} catch(e){}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
$.each(response_tags, function(key, val){ tags[val] = true; });
|
$.each(response_tags, function(key, val){ tags[val] = true; });
|
||||||
tags.redirect_url = true;
|
tags.redirect_url = true;
|
||||||
tags.act = true;
|
tags.act = true;
|
||||||
$.each(resp_obj, function(key, val){ if(tags[key]) ret[key] = val; });
|
$.each(data, function(key, val){ if(tags[key]) ret[key] = val; });
|
||||||
|
|
||||||
if(ret.error != '0') {
|
if(ret.error != '0') {
|
||||||
if ($.isFunction($.exec_xml.onerror)) {
|
if ($.isFunction($.exec_xml.onerror)) {
|
||||||
|
|
@ -318,34 +265,18 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
if($.isFunction(callback_func)) callback_func(ret, response_tags, callback_func_arg, fo_obj);
|
if($.isFunction(callback_func)) callback_func(ret, response_tags, callback_func_arg, fo_obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 모든 xml데이터는 POST방식으로 전송. try-catch문으로 오류 발생시 대처
|
// 모든 데이터는 POST방식으로 전송. try-catch문으로 오류 발생시 대처
|
||||||
try {
|
try {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url : xml_path,
|
url : url,
|
||||||
type : 'POST',
|
type : "POST",
|
||||||
dataType : 'xml',
|
dataType : "json",
|
||||||
data : xml.join('\n'),
|
data : params,
|
||||||
contentType : 'text/plain',
|
|
||||||
beforeSend : function(xhr){ _xhr = xhr; },
|
beforeSend : function(xhr){ _xhr = xhr; },
|
||||||
success : onsuccess,
|
success : onsuccess,
|
||||||
error : function(xhr, textStatus) {
|
error : function(xhr, textStatus) {
|
||||||
waiting_obj.css('display', 'none');
|
waiting_obj.css('display', 'none');
|
||||||
|
alert("AJAX communication error while requesting " + params.module + "." + params.act);
|
||||||
var msg = '';
|
|
||||||
|
|
||||||
if (textStatus == 'parsererror') {
|
|
||||||
msg = 'The result is not valid XML :\n-------------------------------------\n';
|
|
||||||
|
|
||||||
if(xhr.responseText === "") return;
|
|
||||||
|
|
||||||
msg += xhr.responseText.replace(/<[^>]+>/g, '');
|
|
||||||
} else {
|
|
||||||
msg = textStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
console.log(msg);
|
|
||||||
} catch(ee){}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
|
@ -409,8 +340,7 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
* @brief exec_json (exec_xml와 같은 용도)
|
* @brief exec_json (exec_xml와 같은 용도)
|
||||||
**/
|
**/
|
||||||
$.exec_json = window.exec_json = function(action, data, callback_sucess, callback_error){
|
$.exec_json = window.exec_json = function(action, data, callback_sucess, callback_error){
|
||||||
if(typeof(data) == 'undefined') data = {};
|
data = data ? ($.isArray(data) ? arr2obj(data) : data) : {};
|
||||||
|
|
||||||
action = action.split('.');
|
action = action.split('.');
|
||||||
|
|
||||||
if(action.length == 2) {
|
if(action.length == 2) {
|
||||||
|
|
@ -426,7 +356,8 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
|
|
||||||
if(show_waiting_message) $(".wfsr").html(waiting_message).show();
|
if(show_waiting_message) $(".wfsr").html(waiting_message).show();
|
||||||
|
|
||||||
$.extend(data,{module:action[0],act:action[1]});
|
data.module = action[0];
|
||||||
|
data.act = action[1];
|
||||||
|
|
||||||
if(typeof(xeVid)!='undefined') $.extend(data,{vid:xeVid});
|
if(typeof(xeVid)!='undefined') $.extend(data,{vid:xeVid});
|
||||||
|
|
||||||
|
|
@ -435,8 +366,7 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
type: "POST",
|
type: "POST",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
url: request_uri,
|
url: request_uri,
|
||||||
contentType: "application/json",
|
data: data,
|
||||||
data: $.param(data),
|
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
$(".wfsr").hide().trigger('cancel_confirm');
|
$(".wfsr").hide().trigger('cancel_confirm');
|
||||||
if(data.error != '0' && data.error > -1000) {
|
if(data.error != '0' && data.error > -1000) {
|
||||||
|
|
@ -457,22 +387,7 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
},
|
},
|
||||||
error: function(xhr, textStatus) {
|
error: function(xhr, textStatus) {
|
||||||
$(".wfsr").hide();
|
$(".wfsr").hide();
|
||||||
|
alert("AJAX communication error while requesting " + data.module + "." + data.act);
|
||||||
var msg = '';
|
|
||||||
|
|
||||||
if (textStatus == 'parsererror') {
|
|
||||||
msg = 'The result is not valid JSON :\n-------------------------------------\n';
|
|
||||||
|
|
||||||
if(xhr.responseText === "") return;
|
|
||||||
|
|
||||||
msg += xhr.responseText.replace(/<[^>]+>/g, '');
|
|
||||||
} else {
|
|
||||||
msg = textStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
console.log(msg);
|
|
||||||
} catch(ee){}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
|
@ -482,7 +397,7 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
$.fn.exec_html = function(action,data,type,func,args){
|
$.fn.exec_html = function(action, data, type, func, args){
|
||||||
if(typeof(data) == 'undefined') data = {};
|
if(typeof(data) == 'undefined') data = {};
|
||||||
if(!$.inArray(type, ['html','append','prepend'])) type = 'html';
|
if(!$.inArray(type, ['html','append','prepend'])) type = 'html';
|
||||||
|
|
||||||
|
|
@ -497,40 +412,25 @@ function xml2json(xml, tab, ignoreAttrib) {
|
||||||
}, 1000));
|
}, 1000));
|
||||||
if(show_waiting_message) $(".wfsr").html(waiting_message).show();
|
if(show_waiting_message) $(".wfsr").html(waiting_message).show();
|
||||||
|
|
||||||
$.extend(data,{module:action[0],act:action[1]});
|
data.module = action[0];
|
||||||
|
data.act = action[1];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type:"POST",
|
type: "POST",
|
||||||
dataType:"html",
|
dataType: "html",
|
||||||
url:request_uri,
|
url: request_uri,
|
||||||
data:$.param(data),
|
data: data,
|
||||||
success : function(html){
|
success: function(html) {
|
||||||
$(".wfsr").hide().trigger('cancel_confirm');
|
$(".wfsr").hide().trigger('cancel_confirm');
|
||||||
self[type](html);
|
self[type](html);
|
||||||
if($.isFunction(func)) func(args);
|
if($.isFunction(func)) func(args);
|
||||||
},
|
},
|
||||||
error: function(xhr, textStatus) {
|
error: function(xhr, textStatus) {
|
||||||
$(".wfsr").hide();
|
$(".wfsr").hide();
|
||||||
|
alert("AJAX communication error while requesting " + data.module + "." + data.act);
|
||||||
var msg = '';
|
|
||||||
|
|
||||||
if (textStatus == 'parsererror') {
|
|
||||||
msg = 'The result is not valid page :\n-------------------------------------\n';
|
|
||||||
|
|
||||||
if(xhr.responseText === "") return;
|
|
||||||
|
|
||||||
msg += xhr.responseText.replace(/<[^>]+>/g, '');
|
|
||||||
} else {
|
|
||||||
msg = textStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
console.log(msg);
|
|
||||||
} catch(ee){}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
alert(e);
|
alert(e);
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue