我是 Angular 6 的新手,我正在使用 HTTPClient 将我的图像数据发布到 REST 端点。端点上传图片并成功返回上传的图片网址,但在客户端,在控制台中抛出如下错误:

当我在控制台的网络选项卡中看到时,响应没有错误,我得到图像 url。以下是我的代码:
import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { FileUploader, FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
const URL = 'http://localhost:8082/uploadPic';
@Component({
selector: 'app-file-upload',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.css']
})
export class FileUploadComponent implements OnInit {
imgUrl:String=null;
selectedFile: File;
constructor(private http:HttpClient) { }
ngOnInit() {
}
onFileChanged(event) {
this.selectedFile = event.target.files[0]
}
onUpload() {
this.uploadToCloud();
}
uploadToCloud(){
const uploadData = new FormData();
uploadData.append('file', this.selectedFile);
this.http.post('http://localhost:8082/uploadPic', uploadData).subscribe(event => {
console.log('Event:',event); // handle event here
});
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我获得成功的回应。
使用{responseType: 'text'}处理非JSON响应。您可以在此处阅读更多信息:https :
//angular.io/guide/http
您也可以返回一个类似 JSON 的对象作为响应,这将是解决您问题的最值得推荐的方法
| 归档时间: |
|
| 查看次数: |
2090 次 |
| 最近记录: |