预检请求失败 window.fetch api

Preflight Request failing with the window.fetch api

首先让我说它在 POSTMAN 中工作,Chrome 的附加组件 API 客户端。但它在我的实际应用程序中不起作用,我想知道是否有什么我忘记包含在获取请求中的东西。

我有以下要求。

(id, vzId)=>{
            var json = {id: id, vzId: vzId, };
            var request = {
                method: 'POST', 
                body: json,
                headers: new Headers({
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                })
            }
            fetch('http://localhost:8080/notification/markRead',request).then(function(response){
                return response.json();
            }).then(function(json){
                console.log(json);
            })
        }

当请求被触发时,它 returns 我这个错误。

OPTIONS http://localhost:8080/notification/markRead 
Fetch API cannot load http://localhost:8080/notification/markRead.   
Response for preflight has invalid HTTP status code 403

我在我的 chrome 休息客户端 (POSTMAN) 中使用了完全相同的 url 和数据并且它正在工作,但由于某些原因当我使用 FETCH api.任何帮助将非常感激。

万一有人偶然发现了这个并且它可以帮助将来的人,我们找到了答案。我发布的代码没有任何问题。它在 Postman 而不是客户端上工作的原因是因为 Postman 不执行预检请求。客户端(几乎所有网站都会这样做)以 OPTIONS 方法的形式发送了预检请求。后端 Spring 服务器不允许使用选项方法。后端开发人员必须允许 OPTIONS 方法到服务器中的特定路由,而不仅仅是 POST.

因此,如果您在 Postman 上进行测试但无法复制该行为,请记住 Postman 不会发送预检请求。