我正在使用带有“no-cors”模式的fetch polyfill 并获得响应状态 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));
}
知道我做错了什么吗?谢谢!