使用fetch polyfill模式:'no-cors'
Response status = 0 using fetch polyfill with mode: 'no-cors'
我使用'no-cors'模式获取填充,并获得响应状态0。在开发人员工具中,我可以看到响应具有请求的数据。
客户端代码:const BASE_CONFIG = {
credentials: 'include',
mode: 'no-cors'
};
let checkStatus = (response) => {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
};
function GET(url, urlParams={}, config={}) {
let requestConfig = {
method: 'get'
};
Object.assign(requestConfig, BASE_CONFIG, config);
return fetch(url, requestConfig)
.then(checkStatus)
.then(parseJSON);
}
GET('http://other.domain,{})
Beckend nodejs (Express.js)简化响应处理程序:
function getData(req, res) {
var responseData = {data: 'test'};
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.writeHead(200, {"Content-Type": "application/json"});
return res.end(JSON.stringify(responseData));
}
知道我做错了什么吗?谢谢!
no-cors
模式仅针对CDN内容,如脚本、CSS和图片,不能用于获取数据,response.status = 0
是正确的行为
我不知道你是否在使用file:协议,但在这种情况下,你可以得到一个响应状态0。
Chrome和Firefox的原生实现都不支持获取本地文件
参见:https://github.com/github/fetch/pull/92#issuecomment-140665932
现在,不幸的是,文件和ftp url留给读者作为练习。当有疑问时,返回网络错误。
参见:https://fetch.spec.whatwg.org/#basic-fetch(文件和ftp子节)
相关文章:
- fetch() 函数未在 Ubuntu Chromium 浏览器上定义
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- CORS-重定向到第二个GET正在接收的页面
- node.js请求数据事件未在CORS ajax调用中触发
- 带有凭据的角度文件上载(CORS)不起作用
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- SmartGWT数据源和CORS
- CORS-服务器端cookie没有保存在chrome浏览器上
- IE11中的第二个调用取消了第一个Fetch API调用
- CORS:否'访问控制允许来源'header-但是php设置头文件
- Elasticsearch https cors已启用,但仍获得No'访问控制允许来源'标头存在于请求的
- 配置服务器端CORS:ExpressJS No“;访问控制允许起源”;在场
- JavaScript 的 Fetch API 的“同源”和“no-cors”有什么区别?
- 如何使用模式'no-cors'的fetch API获取数据
- 非常困惑的cors与fetch和当一个选项请求作出
- 使用fetch polyfill模式:'no-cors'
- Firebase Fetch - No Access-Control-Allow-Origin
- CORS "No 'Access-Control-Allow-Origin'页眉存在"
- ' fetch ', work with mode ' no-cors '不会调用promise.然后回调
- 试图访问谷歌联系人api使用cors获得No ' access - control - allow - orig