[英]How to access formdata response in angular
Component.ts组件.ts
upload() {
var formData: any = new FormData();
formData.append('file', this.name);
this.http.post('/temp/upload', formData,{responseType: 'text' })
.subscribe((response) => {
console.log('response received is ', response);
if(response.code==400){
alert("Upload success")
}
else{
alert("Upload failed")
}
});}
server.js服务器.js
//upload
router.post('/upload){
if(!error){
res.send({
code: 400,
success: "Upload success"
});
else{
res.send({
code:200
}}
}
The above upload function returns code:400 on success, The problem is I'm able to access "Property 'code' does not exist on type 'string'"上面的上传 function 成功返回代码:400,问题是我能够访问“类型'字符串'上不存在属性'代码'”
Thanks in advance提前致谢
I think you get a string.我想你得到一个字符串。 Then you should parse the JSON string via JSON.parse(response).然后你应该通过 JSON.parse(response) 解析 JSON 字符串。
An other try: responseType: 'text' -> responseType: 'json'另一个尝试: responseType: 'text' -> responseType: 'json'
this.http
.post('/temp/upload', formData, {responseType: 'json'})
.subscribe((response) => {
const res = (typeof response === 'string') ? JSON.parse(response): response;
console.log('response received is ', res);
},
(error) => console.error(error)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.