[英]http post request from Angular to Express server causes 404 error
我在本地向 Azure 函數發出請求
url = 'http://localhost:7071/api/saveGraphDataFlow'
save(body) {
let headers = new HttpHeaders()
headers.append('Content-Type', 'application/json')
return this.httpClient.post(this.url, body, { headers: headers }).pipe(
map(res => {
return res
})
)
}
在我的快速服務器上,我將 cors 添加到響應中
const createHandler = require("azure-function-express").createHandler;
const express = require("express");
const routers = require("./routes/routes");
const app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
next();
});
app.use("/api/", routers);
// Binds the express app to an Azure Function handler
module.exports = createHandler(app);
但是當我發送請求時,我收到此錯誤:
Access to XMLHttpRequest at 'http://localhost:7071/api/saveGraphDataFlow' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
preflight
請求未通過
您可以嘗試從
app.use("/api/", routers);
所以它變成:
app.use("/api", routers);
另外,作為旁注,我還沒有看到您的 API 路由器,因此那里可能有一個額外的斜線或缺失的斜線。 我注意到的另一件事是您正在為路由器導入整個文件夾 (?),因此請確保一次導入文件。 (我沒有看到,所以這可能不是真的)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.