angular - 如何停止在Angular中将+符号转换为空格
问题描述
searchItem 包含一些值,例如 abc+def。当我检查后端时,它会像(abc def)一样。如何修复 .+(加号转换为空格)
search(searchItem: string): Promise<any> {
let params = new HttpParams();
params = params.append('searchItem', searchItem);
return new Promise((resolve, reject) => {
// use For Testing
this.httpClient.get(this.urlService.getApiUrl() + 'test/item',
{
params: params
})
.subscribe(data => {
resolve(data);
}, error => {
console.log('Error: ' + JSON.stringify(error));
reject(error);
});
});
}
解决方案
使用自定义编码器HttpParams
来解决此问题。
import { HttpParameterCodec } from '@angular/common/http';
export class CustomEncoder implements HttpParameterCodec {
encodeKey(key: string): string {
return encodeURIComponent(key);
}
encodeValue(value: string): string {
return encodeURIComponent(value);
}
decodeKey(key: string): string {
return decodeURIComponent(key);
}
decodeValue(value: string): string {
return decodeURIComponent(value);
}
}
然后在你的search()
功能中,
let params = new HttpParams({encoder: new CustomEncoder()});
推荐阅读
- objective-c - 如何在我的 Swift 程序中使用来自 Github 的 Objective-C 代码?
- python - 尽管脚本本身被编译和执行多次,但只调用一次函数
- excel - 收集的数据在外部 excel 关闭时被删除 - excel vba
- jquery - 如何使用从 ajax 调用返回的数据填充特定的 SelectField?
- r - 如何在 R 中进行 lm() 输出 welch t 测试
- c# - asp.net MVC 控制器类可以同时返回 Action Method 和 Json 吗?
- laravel - laravel 中的 show 方法改为返回 index 方法
- python - 为什么我的 Python 脚本在我的 HeapSort 实现上运行得比它应该运行的慢?
- redirect - OpenIdConnect 中 Redirect_Uri 的真正目的是什么?
- c# - 如果内部和外部帐户(facebook、twitter)具有相同的电子邮件地址,如何使用 IdentityServer4 框架实现合并