[英]Failed to load 'endpoint': Request header field If-Modified-Since is not allowed by Access-Control-Allow-Headers in preflight response
I have created one API service using nodejs and it is working fine when I accessing through the browser. 我已经使用nodejs创建了一个API服务,并且在通过浏览器访问时可以正常工作。 But when I'm try to call it from web application (MEAN app), getting the "Failed to load http://localhost:2020/api/posts : Request header field If-Modified-Since is not allowed by Access-Control-Allow-Headers in preflight response" issue. 但是,当我尝试从Web应用程序(MEAN应用程序)调用它时,获取“无法加载http:// localhost:2020 / api / posts :请求标头字段If-Modified-Since被Access-Control不允许-“允许标头显示飞行前响应”问题。
Following code is added in index.js of the API service. API服务的index.js中添加了以下代码。
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
and added following lines in controller of the web app, 并在网络应用程序的控制器中添加了以下几行,
app.config(["$routeProvider", "$httpProvider", function ($routeProvider, $httpProvider) {
$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
}
]);
But no luck for added the above lines. 但是添加以上内容并没有运气。 How to resolve this issue ? 如何解决这个问题?
Thanks. 谢谢。
You need to add If-Modified-Since
to Access-Control-Allow-Headers
in your server code: 您需要在服务器代码中将If-Modified-Since
添加到Access-Control-Allow-Headers
中:
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,If-Modified-Since');
Also the part where you touch HTTP header on client seems useless to me, I don't think you can do anything with that. 在我看来,您在客户端上触摸HTTP标头的那部分似乎也没有用,我认为您无法对此做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.