簡體   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

我已經使用nodejs創建了一個API服務,並且在通過瀏覽器訪問時可以正常工作。 但是,當我嘗試從Web應用程序(MEAN應用程序)調用它時,獲取“無法加載http:// localhost:2020 / api / posts :請求標頭字段If-Modified-Since被Access-Control不允許-“允許標頭顯示飛行前響應”問題。

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();
});

並在網絡應用程序的控制器中添加了以下幾行,

app.config(["$routeProvider", "$httpProvider", function ($routeProvider, $httpProvider) {
    $httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
}
]);

但是添加以上內容並沒有運氣。 如何解決這個問題?

謝謝。

您需要在服務器代碼中將If-Modified-Since添加到Access-Control-Allow-Headers中:

res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,If-Modified-Since');

在我看來,您在客戶端上觸摸HTTP標頭的那部分似乎也沒有用,我認為您無法對此做任何事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM