我正在使用XMLHttpRequest对象上传图像。我正在使用WebStorm作为编辑器。这是代码:
uploadUserImage (files: File[], callback : any): void {
let formData: FormData = new FormData();
let xhr = new XMLHttpRequest();
for (let i = 0; i < files.length; i++) {
formData.append("File", files[i], files[i].name);
}
xhr.open('POST', this._baseUrl + '/users/images', true);
xhr.setRequestHeader('x-jwt-token', this._securityService.getJwt());
xhr.onload = function(){
callback(JSON.parse(this.responseText));
};
xhr.send(formData);
}
问题是在回调中我传递了this.responseText。WebStorm 将此对象标识为 XMLHttpRequestEventTarget。当我尝试运行 NPM 启动时,出现错误:
错误 TS2339:类型"XMLHttpRequestEventTarget"上不存在属性"响应文本"。
我想我需要将其添加到打字中,但我不确定要添加什么。 有什么想法吗?
提前谢谢你。
跟踪此问题时存在问题。在修复之前,您可以使用
xhr.addEventListener('load', function() {
});
以摆脱类型错误。
另一方面,如果你使用的是 angular,你不应该需要直接使用 XMLHttpRequest。 @angular/http 是更好的选择。