您的位置:首页 > Web前端 > AngularJS

Angularjs练手篇——Server编写

2016-04-19 11:47 537 查看
// 统一初始化

var app = angular.module("app", []);

// 配置常量

app.constant("http", "http://192.168.1.1:0000/webStorm/2016V01/");

app.constant("errorMsg", "服务器异常,请稍后重试");

// 配置http发送模式

app.config(["$httpProvider", function($httpProvider) {
delete $httpProvider.defaults.headers.common["X-Requested-With"];
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.transformRequest = function(data) {
// 当参数不为空的时候
if (data) {
return $.param(data);
}
};
} ]);

// 配置service

app.service("configService", function($http, http, errorMsg) {

// post获取数据
this.post = function(url, params, success) {
$http.post(http + url, params).success(function(resp) {
if (resp.status) {
success(resp);
} else {
layer.msg(resp.data);
}
}).error(function(resp) {
layer.msg(errorMsg);
});
}
// get获取数据 
this.get = function(url, success) {
$http.get(http + url).success(function(resp) {
success(resp);
}).error(function(resp) {
layer.msg(errorMsg);
});
}

//获取地址栏信息
this.getUrlParam = function(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null)
return unescape(r[2]);
return null;
}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: