小编Rai*_*gan的帖子

TypeError:无法在'Window'上执行'fetch':无效的值

我尝试使用fetch从后端调用使用react,而不使用libs(例如Axios).所以我创建了这个函数:

export function api(url, method, body, isHeaderContentType,isRequestHeaderAuthentication,header, succesHandler, errorHandler) {
const prefix = 'link';
console.log("url:",prefix+url);
const contentType = isHeaderContentType ? {
    'Content-Type': 'application/json',
} : {};
const auth = isRequestHeaderAuthentication
    ? {
        Authorization: `Bearer ${AuthUtils.getTokenUser}`,
    }
    : {};
fetch(prefix + url, {
    method,
    headers: {
        ...contentType,
        ...auth,
        ...header,

    },
    protocol:'http:',
    body,
})
    .then(response => {
        response.json().then(json => {
            if (response.ok) {
                console.log("method", json);
                if (succesHandler) {
                    succesHandler(json)
                }
            } else {
                return Promise.reject(json)
            }
        })
    })
    .catch(err => {
        console.log("error",`${url} …
Run Code Online (Sandbox Code Playgroud)

javascript fetch reactjs

16
推荐指数
4
解决办法
2万
查看次数

标签 统计

fetch ×1

javascript ×1

reactjs ×1