【发布时间】:2020-04-27 06:18:40
【问题描述】:
尝试使用 JSON 解析,但出现此错误: “对象”类型的参数不能分配给“字符串”类型的参数。
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-uploader',
templateUrl: './uploader.page.html',
styleUrls: ['./uploader.page.scss'],
})
export class UploaderPage implements OnInit {
imageURL: string
constructor(public http: HttpClient) { }
ngOnInit() {
}
fileChanged(event) {
const files = event.target.files
const data = new FormData()
data.append('file', files[0])
data.append('UPLOADCARE_STORE', '1')
data.append('UPLOADCARE_PUB_KEY', '12d3f0b0b65cb448aa6b')
this.http.post('https://upload.uploadcare.com/base/', data).subscribe(event => {
console.log(event)
this.imageURL = JSON.parse(event).file
})
}
}
在(事件)下的this.imageURL = JSON.parse(event).file 行中,我收到了该错误。可能是什么原因以及如何解决。
HTML:
<ion-header>
<ion-toolbar>
<ion-title>Upload Image</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<div class="camera"> </div>
<input type="file" (change)="fileChanged($event)"/>
<img *ngIf="imageURL" src="https://ucarecdn.com/{{ imageURL}}/"/>
</ion-content>
【问题讨论】:
-
这里
JSON.parse()需要什么?event响应是 JSON 格式的字符串吗? -
@MichaelD 我需要在 uploadcare 中上传的网址,以便我可以在我的 html 代码中显示照片
-
我已经发布了答案。请看看它是否适合你。