@Chiang
2021-09-25T17:50:01.000000Z
字数 3694
阅读 530
2021-09
ecshop
Ajax.call
先来看看文件在哪儿
transport.js
Ajax.call('goods.php?act=gotopage', 'page=' + page + '&id=' + id, gotoBuyPageResponse, 'GET', 'JSON');
/* 定义两个别名 */
var Ajax = Transport;
Ajax.call = Transport.run;
/* *
* 调用此方法发送HTTP请求。
*
* @public
* @param {string} url 请求的URL地址
* @param {mix} params 发送参数
* @param {Function} callback 回调函数
* @param {string} ransferMode 请求的方式,有"GET"和"POST"两种
* @param {string} responseType 响应类型,有"JSON"、"XML"和"TEXT"三种
* @param {boolean} asyn 是否异步请求的方式
* @param {boolean} quiet 是否安静模式请求
*/
run : function (url, params, callback, transferMode, responseType, asyn, quiet){
/* 处理用户在调用该方法时输入的参数 */
params = this.parseParams(params);
transferMode = typeof(transferMode) === "string" && transferMode.toUpperCase() === "GET" ? "GET" : "POST";
if (transferMode === "GET")
{
var d = new Date();
url += params ? (url.indexOf("?") === - 1 ? "?" : "&") + params : "";
url = encodeURI(url) + (url.indexOf("?") === - 1 ? "?" : "&") + d.getTime() + d.getMilliseconds();
params = null;
}
responseType = typeof(responseType) === "string" && ((responseType = responseType.toUpperCase()) === "JSON" || responseType === "XML") ? responseType : "TEXT";
asyn = asyn === false ? false : true;
/* 处理HTTP请求和响应 */
var xhr = this.createXMLHttpRequest();
try
{
var self = this;
if (typeof(self.onRunning) === "function" && !quiet)
{
self.onRunning();
}
xhr.open(transferMode, url, asyn);
if (transferMode === "POST")
{
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
if (asyn)
{
xhr.onreadystatechange = function ()
{
if (xhr.readyState == 4)
{
switch ( xhr.status )
{
case 0:
case 200: // OK!
/*
* If the request was to create a new resource
* (such as post an item to the database)
* You could instead return a status code of '201 Created'
*/
if (typeof(self.onComplete) === "function")
{
self.onComplete();
}
if (typeof(callback) === "function")
{
callback.call(self, self.parseResult(responseType, xhr), xhr.responseText);
}
break;
case 304: // Not Modified
/*
* This would be used when your Ajax widget is
* checking for updated content,
* such as the Twitter interface.
*/
break;
case 400: // Bad Request
/*
* A bit like a safety net for requests by your JS interface
* that aren't supported on the server.
* "Your browser made a request that the server cannot understand"
*/
//alert("XmlHttpRequest status: [400] Bad Request");//by dmc
break;
case 404: // Not Found
//alert("XmlHttpRequest status: [404] \nThe requested URL "+url+" was not found on this server.");// by dmc
break;
case 409: // Conflict
/*
* Perhaps your JavaScript request attempted to
* update a Database record
* but failed due to a conflict
* (eg: a field that must be unique)
*/
break;
case 503: // Service Unavailable
/*
* A resource that this request relies upon
* is currently unavailable
* (eg: a file is locked by another process)
*/
//alert("XmlHttpRequest status: [503] Service Unavailable"); //by dmc
break;
default:
//alert("XmlHttpRequest status: [" + xhr.status + "] Unknow status."); //by dmc
}
xhr = null;
}
}
if (xhr != null) xhr.send(params);
}
else
{
if (typeof(self.onRunning) === "function")
{
self.onRunning();
}
xhr.send(params);
var result = self.parseResult(responseType, xhr);
//xhr = null;
if (typeof(self.onComplete) === "function")
{
self.onComplete();
}
if (typeof(callback) === "function")
{
callback.call(self, result, xhr.responseText);
}
return result;
}
}
catch (ex)
{
if (typeof(self.onComplete) === "function")
{
self.onComplete();
}
}
},
/**
* @access public
* @param
* @return void
*/
function make_json_result($content, $message='', $append=array())
{
make_json_response($content, 0, $message, $append);
}
/**
* 创建一个JSON格式的数据
*
* @access public
* @param string $content
* @param integer $error
* @param string $message
* @param array $append
* @return void
*/
function make_json_response($content='', $error="0", $message='', $append=array(), $go2url = '')
{
include_once(ROOT_PATH . 'includes/cls_json.php');
$json = new JSON;
$res = array('error' => $error, 'message' => $message, 'content' => $content, 'go2url' => $go2url);
if (!empty($append))
{
foreach ($append AS $key => $val)
{
$res[$key] = $val;
}
}
$val = $json->encode($res);
exit($val);
}