版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014490083/article/details/79353312
代码片段
'use strict';
var conf = {
serverHost = ''
};
var _mm = {
//网络请求功能
request : function(param){
var _this = this;
$.ajax({
type : param.method || 'get',
url : param.url || '',
dataType : param.type || 'json',
data : param.data || '',
success : function(res){
//请求成功
if(0 === res.status){
typeof param.success === 'function' && param.success(res.data, res.msg);
}
//请求数据错误
else if(1 === res.status){
typeof param.error === 'function' && param.error(res.message);
}
},
error : function(err){
typeof param.error === 'function' && param.error(res.statusText);
}
});
},
//获取服务器地址
getServerUrl : function(path){
return conf.serverHost + path;
},
//获取 url 参数
getUrlParam : function(name){
var reg = new RegExp('(^|&)'+ name + '=([^&]*)(&|$)');
var result = window.location.search.substr(1).match(reg);
return result ? decodeURIComponent(result[2]) : null;
}
};
module.exports = _mm;
解释
a.typeof
:操作符返回一个字符串,指示未经计算的操作数的类型。
b.var reg = new RegExp('(^|&)'+ name + '=([^&]*)(&|$)')
:正则表达式子。
c.window.location.search.substr(1).match(reg)
:
window.location.search
:返回从问号到URL末尾的所有内容(?q=javascript);substr(1)
:返回一个从指定位置开始的指定长度的子字符串(这里设置为1,是为了把url中的?号去掉);match(reg)
:根据正则表达式匹配需要的数据。