0

我正在尝试 whatwg-fetch (用于 Fetch API 的 polyfill),并且在执行 POST 时,会执行预检。但是由于在将 OPTIONS 发送到 REST 服务时未发送凭据,因此我收到了“未经授权”的响应。

return fetch('http://localhost:8080/activity', {
  credentials: 'include',
  method: 'POST',
  mode: 'cors',
  body: JSON.stringify(activity),
  headers: new Headers({ 'Content-Type': 'application/json' })
});
4

2 回答 2

1

以我的情况为答案。我相信它会帮助你:

export function doSearchRequest (filters) {
    let token = $('meta[name="csrf-token"]').attr('content');
    return (fetch('/services/search/message', { 
            method: "POST",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Cache': 'no-cache',
                '_token' : token,
                'X-CSRF-Token' : token,
                'X-XSRF-TOKEN' : token
            },
            credentials: 'include',
            body: JSON.stringify(filters)
        })
        .then(response => response.json())
        .then(function(json) {
            return json;
        })
    );    
}
于 2017-03-21T03:54:24.000 回答
0

这是有关您的案例的所有信息https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request也在这里https://fetch.spec.whatwg.org/#cors-preflight-fetch。浏览器发送预检 OPTIONS 请求,因为您使用跨域请求

于 2017-10-11T12:59:14.590 回答