这是我在nodejs上启用CORS的方式。但是前端更新记录的请求似乎并没有起作用。但是获取,发布,删除命令都有效。
app.use((req, res, next) => {//Middleware for CORS. Code to allow requests from any url
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", 'GET, POST, OPTIONS, PUT, DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept ");
next();
})
这是我在Nodejs上执行update语句的方式
backend.put('/updateworkorder/:id', (req, res) => {
console.log(req.body);
let sql = "UPDATE workorders SET ? WHERE ?"
db.query(sql, [{accountNumber: req.body.accountNumber,
customerName: req.body.customerName,
packageName: req.body.packageName,
addons: req.body.addons,
campaignName: req.body.campaignName,
notes: req.body.notes,
entryDate: new Date(req.body.entryDate),
crew: req.body.crew,
postDate: new Date(req.body.postDate),
status: req.body.status
},
{id: req.body.id}], (err, result) => {
if (err) throw err;
// console.log(result);
res.send(result);
});
});
这是Angular中的调用部分
updateWorkOrder(workOrder) {
// console.log(workOrder.id);
let headers = new HttpHeaders();
headers.append('Content-Type' , 'application/json');
return this.http.put(this.BASE_URL + '/updateworkorder/' + workOrder.id, {headers: headers});
}
答案 0 :(得分:2)
根据您的方法映射,您的URL应包含要由映射模板识别的ID。服务器无法识别URL模板并返回404。 要创建有效的URL并传递数据以进行请求,您应该将UI上的方法更改为
this.http.put<IWorkOrders>(this.BASE_URL + '/updateworkorder/' + workOrder.id, workOrder);
答案 1 :(得分:0)
您错过了正在投放的网址中的ID,这就是您收到404 Not Found错误的原因。在日志中,您可以看到您正在调用的URL是'/ backend / updateworkorder /',它应该是'/ backend / updateworkorder / your-id'。
将您的电话更新为:
this.http.put<IWorkOrders>(this.BASE_URL + '/updateworkorder/' + workOrder.id);