javascript 如何在 angularjs $http 请求中获取请求 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31244744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get request url in an angularjs $http request
提问by Jahongir Rahmonov
I have this request:
我有这个要求:
$http({
method: 'get',
url: '/api/items/',
params: {a: 1, b: 2, c: 3}
}).success(function (response) {
console.log(response);
}).error(function (err) {
console.log(err);
});
Is there a way, to fetch the url it used to make the request after the request has been made(in the callback or otherwise)?
有没有办法在发出请求后(在回调中或以其他方式)获取用于发出请求的 url?
I would want the output:
我想要输出:
http://www.example.org/api/items?a=1&b=2&c=3
Herethe same thing is done with jquery.
这里用 jquery 做同样的事情。
回答by riptidebyte
The success handler gets 4 parameters passed into it:
成功处理程序将 4 个参数传递给它:
$http
.get({ url: '/someUrl', params: { q: 3 } })
.success(function(data, status, headers, config) {});
The fourth parameter config
has the following properties:
第四个参数config
具有以下属性:
{
method: "GET",
url: {
url: '/someUrl',
params: {
q: 3
}
}
}
You can use window.location.origin
to get the base url and build a simple function to concat it all together.
您可以使用window.location.origin
获取基本 url 并构建一个简单的函数来将它们连接在一起。
This function should yield the expected response:
这个函数应该产生预期的响应:
var baseUrl = window.location.origin;
var query = [];
Object.keys(config.url.params || {}).forEach(function (key) {
var val = config.url.params[key];
query.push([key, val].join('=')); // maybe url encode
});
var queryStr = query.join('&');
var fullPath = baseUrl + config.url.url + '?' + queryStr;
Unfortunately, this function will only work as long as the parameters are passed in the format described above. If you pass them in a different format, you'll have to modify this function a bit. You can use this as a playground.
不幸的是,这个函数只有在以上述格式传递参数时才能工作。如果您以不同的格式传递它们,则必须稍微修改此函数。您可以将其用作游乐场。
Afaik, there is no simpler way. At least a feature request exists.
回答by Firefly
This config parameter of the callback has the url as a property:
回调的此配置参数将 url 作为属性:
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
console.log(config.url);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Based on: https://docs.angularjs.org/api/ng/service/$http
回答by user1608841
You can use http request Interceptor. Configure the Interceptor in config function using $httpProvider Whrein for request Interceptor you can able to see the URL and params that you passed with URL prior to making the request
您可以使用 http 请求拦截器。使用 $httpProvider Whrein 为请求拦截器在配置函数中配置拦截器,您可以在发出请求之前查看与 URL 一起传递的 URL 和参数
回答by ryanrain
try just looking at the XHR Request urls in your browser dev tools
尝试在浏览器开发工具中查看 XHR 请求 url