angular Unknown Error 请求错误0
2021-01-27 本文已影响0人
survivorsfyh
大致场景如下:
正常使用 angular 的 Service 时候怎么 set 都不能正常存储数据字段,经过几经测试后发现是 set 前 URL 获取参数的时候数据字段格式不兼容,fuck !!!
如下是实现的具体 code:
import { Injectable } from '@angular/core';
import { UserInfo } from '../data/userInfo';
@Injectable({
providedIn: 'root'
})
export class UserService {
user: UserInfo = {
caId:'',
token:'',
tissueId:'',
userName:'',
};
constructor() { }
setUserInfo(userInfo: UserInfo){
this.user = userInfo;
}
get userInfo(){
return this.user;
}
}
const caId = this.route.snapshot.queryParams.caid;
const token = this.route.snapshot.queryParams.token;
const tissueId = this.route.snapshot.queryParams.tissueid;
const username = this.route.snapshot.queryParams.username;
this.userInfo.caid = caId;
this.userInfo.token = token;
this.userInfo.tissueid = tissueId;
this.userInfo.username = username;
this.userService.setUserInfo(this.userInfo);
this.rotationService.getRotationUserList(this.pageSize, encodeURI(this.search), this.pageIndex).subscribe(data => {
this.loadingTable = false;
console.log(JSON.stringify(data));
}, (error) => {
this.loadingTable = false;
this.msg.error(error);
}, () => {
this.loadingTable = false;
});
问题原因
如上是实现的部分,但是发起请求一直异常;后来尝试把数据字段写死进行尝试,就在尝试到使用中文字段的时候,嗯 gg 了!!!英文字符均 ok,唯独汉字他瞄的凉了!!!
解决办法
将中文字段部分存储的时候先 encodeURI,在需要调用的地方再 decodeURIComponent 即可 。。。
千算万算没有算到栽到汉字上了,也建议传参的时候汉字部分都先 encodeURI 处理一下最好 。。。
以上便是此次分享的全部内容,希望能对大家有所帮助!