即时消息使nodejs成角度的API调用,如下所示>
public TUpdation(data): Observable<any>{
const headers = new HttpHeaders();
headers.set('content-type', null);
headers.set('Accept', 'multipart/form-data');
headers.set('Access-Control-Allow-Origin',"*");
return this.http.post(API.UPDATION, data, { headers });
}
在node.js部分中,我试图接收下面的函数。
exports.Updation = async (req,res,next) => {
console.log("req.body : ",req.body); //Not getting the contents (empty)
var classTM = req.body.checkbox2;
var _entityclass;
try{
if(entityclass){
_entityclass = "EE";
}
else if(entityclass){
_entityclass = "TM";
}
else{
_entityclass = "TMP";
}
try{
console.log("req.body --- !!! ",req.body); // Here Im able to get the contents
}
catch(e){
}
}
catch(e){
}
}
我的问题是我一开始无法获得体内的成分。无论我要做什么,都无法将re.body内容放在首位。在第二次尝试中,我可以提取。有人可以帮我吗?我做错了什么?
也许您可以尝试以下代码:
public TUpdation(data): Observable < any > {
const headers = new HttpHeaders();
// set content-type: `application/json`
headers.set('content-type', 'application/json');
// you can comment: multipart/form-data, if it's still does not work
headers.set('Accept', 'multipart/form-data');
headers.set('Access-Control-Allow-Origin', "*");
// make sure your "data" is not an empty object
// you can console.log(data);
return this.http.post(API.UPDATION, data, {
headers
});
}
在使用上述代码在Angular应用中更改代码之后,现在,请确保已将您添加body-parser
到Express应用中。
您可以body-parser
使用以下代码设置您的代码:
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
您可以在上面复制该代码,然后放入app.js
或中server.js
。
希望对您有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句