Ionic Angular - 如何用 URI 前缀替换特殊字符
Ionic Angular - How to replace Special Characters with a Prefix for URI
我是 Ionic 的新手,我从一个最近离开我工作的女孩那里接手了一个项目。
我收到一个缺陷,当用户输入“N/A”来搜索订单时,它会在搜索时破坏 URI。一位共同开发者建议将“/”替换为“%2F”将在 URI 中解决此问题。
对于长期修复,我希望能够读取用户输入的内容,如果其中包含任何特殊字符,则将其替换为此前缀?
这是我的一些代码:
async getOrder() {
if ( this.searchValue.length < 1) {
console.log('search is empty');
this.alertService.emptyInput();
} else {
/**
* Setup loading controller
*/
const loading = await this.loadingController.create({
message: 'Searching..'
});
await loading.present();
// Get access token from storage
this.storage.get(ACCESS_TOKEN).then((token) => {
console.log('token from storage: ', token);
if (this.searchValue === 'N/A') {
this.searchValue = 'N%2FA';
}
const urlOrderNo = this.apiUrl + 'ordersearch/' + this.searchValue;
因此,例如我添加了 = 'N/A',但我还需要每次检查字符串,因为订单号也可能包含特殊字符。
有人能帮帮我吗?
您可以使用以下任何一种方法:
1. escape()
这个方法不会编码下面的@<em>/+
2. encodeURIComponent()
这个方法不会编码下面的~!</em>()'
我更喜欢第二种方法encodeURIComponent()
所以替换
if (this.searchValue === 'N/A') {
this.searchValue = 'N%2FA';
}
const urlOrderNo = this.apiUrl + 'ordersearch/' + this.searchValue;
和
const urlOrderNo = this.apiUrl + 'ordersearch/' + encodeURIComponent(this.searchValue);
我是 Ionic 的新手,我从一个最近离开我工作的女孩那里接手了一个项目。 我收到一个缺陷,当用户输入“N/A”来搜索订单时,它会在搜索时破坏 URI。一位共同开发者建议将“/”替换为“%2F”将在 URI 中解决此问题。
对于长期修复,我希望能够读取用户输入的内容,如果其中包含任何特殊字符,则将其替换为此前缀?
这是我的一些代码:
async getOrder() {
if ( this.searchValue.length < 1) {
console.log('search is empty');
this.alertService.emptyInput();
} else {
/**
* Setup loading controller
*/
const loading = await this.loadingController.create({
message: 'Searching..'
});
await loading.present();
// Get access token from storage
this.storage.get(ACCESS_TOKEN).then((token) => {
console.log('token from storage: ', token);
if (this.searchValue === 'N/A') {
this.searchValue = 'N%2FA';
}
const urlOrderNo = this.apiUrl + 'ordersearch/' + this.searchValue;
因此,例如我添加了 = 'N/A',但我还需要每次检查字符串,因为订单号也可能包含特殊字符。
有人能帮帮我吗?
您可以使用以下任何一种方法:
1. escape()
这个方法不会编码下面的@<em>/+
2. encodeURIComponent()
这个方法不会编码下面的~!</em>()'
我更喜欢第二种方法encodeURIComponent()
所以替换
if (this.searchValue === 'N/A') {
this.searchValue = 'N%2FA';
}
const urlOrderNo = this.apiUrl + 'ordersearch/' + this.searchValue;
和
const urlOrderNo = this.apiUrl + 'ordersearch/' + encodeURIComponent(this.searchValue);