빈 몸의 지방 API의 결과로 보내기 요청, 그러나(Angular4/MEAN) 예를 들어 내가 항목 API에 데이터를 게시하기 위해 노력하고있어
"data": {
"title": "stack",
"notes": "asdsad",
"time": "19:02",
"meridian": "PM",
"type": "Education",
"_id": "5a2f02d3bba3640337bc92c9",
"days": {
"Monday": true,
"Tuesday": false,
"Wednesday": false,
"Thursday": false,
"Friday": false,
"Saturday": false,
"Sunday": false
}
}
는 데이터를 게시 할 HttpClient를 사용하는 경우
this.http.post("http://localhost:3000/api/items",
JSON.stringify(item))
.subscribe(
(val) => {
console.log("POST call successful value returned in body",
val);
},
response => {
console.log("POST call in error", response);
},
() => {
console.log("The POST observable is now completed.");
});
항상 API의 항목 컨트롤러에서 잘못된 요청 400 응답을받습니다. 당신이 몸을 볼 수있는
_startTime: 2017-12-11T22:35:18.204Z,
_remoteAddress: '::1',
body: {},
secret: undefined,
cookies: {},
signedCookies: {},
route:
Route {
path: '/',
stack: [ [Object], [Object] ],
methods: { post: true } } }
{}
을 다음과 같이 이미 명시 콘솔에서 너무
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
나의 app.js에 BodyParser을 설정 한
exports.createItem = async function(req, res, next){
// Req.Body contains the form submit values.
console.log(req);
console.log(req.body);
var item = {
id: req.body.id,
title: req.body.title,
days: req.body.days,
notes: req.body.notes,
time: req.body.time,
meridian: req.body.meridian,
type: req.body.type,
completed: req.body.completed,
}
try{
// Calling the Service function with the new object from the Request Body
var createdItem = await ItemService.createItem(item)
return res.status(201).json({status: 201, data: createdItem, message: "Succesfully Created Item"})
} catch(e){
console.log(e);
//Return an Error Response Message with Code and the Error Message.
return res.status(400).json({status: 400, message: "Item Creation was Unsuccesfull"})
}
}
는, 출력은 이 항목이 비어있어 항목을 만들 수 없습니다. 필자는 Postman을 사용하여 이것을 테스트했으며 url 인코딩 된 양식 데이터와 원시 JSON 데이터를 보낼 때 게시물이 성공하고 200을 반환합니다. 그러나 응용 프로그램의 요청을받을 수는 없습니다.
저는 몇 시간 동안이 문제로 고생하고있어 많은 도움을 받았습니다.
은 JSON 이외의 문자열을 예상하는 서버 측입니까? – haifzhan