比如下面angular这种url只写后半段
提交后如何生成前面这段地址的?
直接上代码,纯个人设计,你可以随意增删改,只是提供个思路
`$angular.factory("commonHeader", function () {
return "http://xx.xx.xx.xx:8081";
});
$angular.service("commonHttp", function ($http, $httpParamSerializer, commonHeader) {
this.url = "";
this.sendData = "";
this.request = function (successCallback, errorCallback) {
if (typeof (errorCallback) != "function") {
errorCallback = function () {
};
}
if (!this.sendData){
this.sendData = {};
}
$http({
method: "POST",
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
url: commonHeader + this.url,
data: $httpParamSerializer(this.sendData)
}).then(function (response) {
successCallback(response)
}, errorCallback());
}
});`
`$angular.factory("XXXX", function (commonHttp) {
return {
url : "/xx/xx",
sendData: commonHttp.sendData,
request: commonHttp.request
};
});`
关键字 interceptors
var app = angular.module('myApp', []);
app.config(function ($httpProvider) {
$httpProvider.interceptors.push(function ($q) {
return {
'request': function (config) {
config.url = 'http://localhost:8080/XVIEW/' + config.url;
return config || $q.when(config);
}
}
});
});
app.controller('myCtrl', function ($scope,$http) {
$http.get('stockpledge/UpdateStockpledge').success(function (data) { alert(data) }).error(function (fail) {
});
});
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
跟angular无关。这个由浏览器完成
请求url不写绝对路径时候,规则如下:(以当前问题链接为例子)
页面链接
https://segmentfault.com/q/1010000010783953?_ea=2414431
$.ajax('/a.php') - 访问 https://segmentfault.com/a.php
$.ajax('a.php') - 访问 https://segmentfault.com/q/a.php