预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 If-None-Match

Posted

技术标签:

【中文标题】预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 If-None-Match【英文标题】:Request header field If-None-Match is not allowed by Access-Control-Allow-Headers in preflight response 【发布时间】:2016-05-31 14:09:47 【问题描述】:

我在使用 dojo/store/JsonRest 发布到 REST API 时收到以下错误。 我正在使用"X-Requested-With": null 以避免预检请求,但我仍然收到此错误。

API 完全支持 CORS。

知道如何解决吗?

请求头域 If-None-Match 不允许 预检响应中的 Access-Control-Allow-Headers。

        var store = new JsonRest(
            target: 'https://api.xxx.com/data',
            headers: 
                "Authorization": 'Bearer ' + 'd4c72611fc43ab44a46344d907a2b96964df2c91',
                "X-Requested-With": null // no prefligh
            
        );
        store.get('1-00').then(function (data) 
            // ok works here
            console.log('get', data)
        );
        // post request
        store.add(name:'test').then(function (data) 
            // error here
            console.log('add', data)
        );

【问题讨论】:

【参考方案1】:

我能够在 JsonRest 的标头中使用 "If-None-Match": null 解决此问题。

关于"If-None-Math" 的有趣文档可以在HTTP/1.1 spec 上找到。


var store = new JsonRest(
        target: 'https://api.xxx.com/data',
        headers: 
            "Authorization": 'Bearer ' + 'd4c72611fc43ab44a46344d907a2b96964df2c91',
            "X-Requested-With": null`, // no prefligh
            "If-None-Match": null // solve my issue
        
    );
    store.get('1-00').then(function (data) 
        // ok works here
        console.log('get', data)
    );
    // post request
    store.add(name:'test').then(function (data) 
        // ok works here
        console.log('add', data)
    );

【讨论】:

以上是关于预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 If-None-Match的主要内容,如果未能解决你的问题,请参考以下文章

预检响应没有 HTTP ok 状态。 Angular 6 中的 403

OAuth 中的 CORS:对预检请求的响应未通过访问控制检查

预检响应中的 Access-Control-Allow-Headers 不允许授权

预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段内容类型

预检响应中的 Access-Control-Allow-Header 中的允许请求标头字段

预检响应中的 Access-Control-Allow-Methods 不允许方法 PATCH