[英]How to access formdata response in angular
組件.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")
}
});}
服務器.js
//upload
router.post('/upload){
if(!error){
res.send({
code: 400,
success: "Upload success"
});
else{
res.send({
code:200
}}
}
上面的上傳 function 成功返回代碼:400,問題是我能夠訪問“類型'字符串'上不存在屬性'代碼'”
提前致謝
我想你得到一個字符串。 然后你應該通過 JSON.parse(response) 解析 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.