小编Rai*_*gey的帖子

响应状态= 0使用fetch polyfill with mode:'no-cors'

我正在使用fetch polyfill和'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,{})
Run Code Online (Sandbox Code Playgroud)

Beckend nodejs(Express.js)简化的响应处理程序:

function getData(req, res) {
    var responseData = {data: 'test'};
    res.header("Access-Control-Allow-Origin", "*"); …
Run Code Online (Sandbox Code Playgroud)

javascript get http fetch-api

8
推荐指数
1
解决办法
4095
查看次数

标签 统计

fetch-api ×1

get ×1

http ×1

javascript ×1