天天看点

Angularjs练手篇——Server编写

// 统一初始化

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;

}

});