简体   繁体   English

无法加载“端点”:在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段If-Modified-Since

[英]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.

相关问题 预检响应中的 Access-Control-Allow-Headers 不允许 Angularjs 请求标头字段 Access-Control-Allow-Headers - Angularjs Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response 请求标头字段在飞行前响应中,Access-Control-Allow-Headers不允许进行授权。 (节点,反应,axios) - Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response. (node, react, axios) Node JS - CORS - 请求 header 预检响应中的 Access-Control-Allow-Headers 不允许字段授权 - Node JS - CORS - Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response NodeJS + ExpressJS:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 - NodeJS + ExpressJS: Request header field not allowed by Access-Control-Allow-Headers in preflight response 允许使用restify的选项方法 - 请求头字段预检响应中的Access-Control-Allow-Headers不允许授权 - allowing options method with restify - Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段身份验证 - Request header field authentication is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段内容类型 - Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response CORS 错误:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段授权 - CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 ack 即使服务器已经允许它 - Request header field ack is not allowed by Access-Control-Allow-Headers in preflight response even the server already allowing it 在飞行前响应中,节点Js请求标头字段Access-Control-Allow-Headers不允许Access-Control-Allow-Origin - Node Js Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM