[英]How to encode a URL in an AngularJS controller
I'm having a bit of a problem trying to submit URLs that I'm building dynamically in my AngularJS controller code, and then encoding using encodeURIComponent()我在尝试提交我在 AngularJS 控制器代码中动态构建的 URL,然后使用 encodeURIComponent() 进行编码时遇到了一些问题
Here's a URL example built out dynamically (prior to encoding, and will not submit via Fiddler):这是一个动态构建的 URL 示例(在编码之前,不会通过 Fiddler 提交):
http://localhost:49479/api/aggr?sid=f68f52614800393fdbef22cc55a7d3d0fea10e655fff6e7573ca&kri=[CDSStress A]:[USD 10Y X -1.25],[CDSStress A]:[USD 1Y X 1.25]&aggrFunc=SUM([CDSStress A]:[USD 10Y X -1.25]),SUM([CDSStress A]:[USD 1Y X 1.25])&dim=Counterparty
Same URL, manually encoded with %20 (which does submit to API layer via Fiddler):相同的 URL,用 %20 手动编码(它确实通过 Fiddler 提交到 API 层):
http://localhost:49479/api/aggr?sid=f68f52614800393fdbef22cc55a7d3d0fea10e655fff6e7573ca&kri=[CDSStress%20A]:[USD%201Y%20X%201.25],[CDSStress%20A]:[USD%201Y%20X%20-1.25]&aggrFunc=SUM([CDSStress%20A]:[USD%201Y%20X%201.25]),SUM([CDSStress%20A]:[USD%201Y%20X%20-1.25])&dim=Counterparty
However, encoding via encodeURIComponent() does not submit to my API layer via Fiddler.但是,通过 encodeURIComponent() 进行编码不会通过 Fiddler 提交给我的 API 层。 Here's the coded URL:
这是编码后的网址:
http%3A%2F%2Flocalhost%3A49479%2Fapi%2Faggr%3Fsid%3Df68f52614800393fdbef22cc55a7d3d0fea10e655fff6e7573ca%26kri%3D%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%201.25%5D%2C%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%20-1.25%5D%26aggrFunc%3DSUM(%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%201.25%5D)%2CSUM(%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%20-1.25%5D)%26dim%3DCounterparty
and my angular controller code which builds out the URL and submit to datacontext layer :和我的角度控制器代码,它构建了 URL 并提交到数据上下文层:
function sendAggrRequest(kriList, aggrFunc, dim) {
var results = [];
var rageVars = $rootScope.rageSessionVars;
var url = "http://" + rageVars.domainName + ":" + rageVars.port + "/api/aggr?sid=" + rageVars.sessionID +
"&kri=" + kriList + "&aggrFunc=" + aggrFunc + "&dim=" + dim;
url="http://localhost:49479/api/aggr?sid=a74b9822cf5e0e75b0d8ff0c25981a573606893150348d6cad80&kri=[CDSStress%20A]:[USD%201Y%20X%201.25],[CDSStress%20A]:[USD%201Y%20X%20-1.25]&aggrFunc=SUM([CDSStress%20A]:[USD%201Y%20X%201.25]),SUM([CDSStress%20A]:[USD%201Y%20X%20-1.25])&dim=Counterparty"
datacontext.sendAggrRequestToServer(encodeURIComponent(url)).then(function (data) {
if (data.status == 'FAIL') {
if (data.messages.length > 0) {
logErr("Error retrieving KRI list: " + data.messages[0]);
return;
}
}
else {
results = data.data;
}
});
}
If you can provide some advice on the best way to encode URLs, that would be great.如果您能提供一些关于编码 URL 的最佳方式的建议,那就太好了。
thank you.谢谢你。
Bob鲍勃
Try尝试
encodeURI(uri)?
or may be you can just regex replace
space
with %20
或者你可以用
%20
用regex replace
space
I've had no issues using encodeURIComponent inside AngularJS:我在 AngularJS 中使用 encodeURIComponent 没有任何问题:
var params = $location.search();
var someThingEncoded = encodeURIComponent(params.someThing);
If things are funky, you may want to try also $log.log.如果事情很奇怪,您可能还想尝试 $log.log。 For example, encodeURI may not work for a url segment, but encodeURIComponent still may, so you can try logging both to console and compare.
例如, encodeURI 可能不适用于 url 段,但 encodeURIComponent 仍然可以,因此您可以尝试将两者都记录到控制台并进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.