报如下错误
request.js:60 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Invalid value
写了个fetch的工具类
代码如下:
import 'whatwg-fetch'; //我引入fetch了啊
import {isEmpty} from './util/string';
import {BizException} from './exception';
import config from './config';
/*通用请求方法*/
const request = (method, url, headers, body) => {
console.log('method', method);
console.log('url', url);
console.log('headers', headers);
console.log('body', body);
if (isEmpty(method)) {
throw new BizException("method为空");
}
if (isEmpty(url)) {
throw new BizException("url为空");
}
if (method === 'GET') {
if (body != null) {
throw new BizException("get方法中body必须为空");
}
}
if (method === 'POST ') {
if (body !== undefined && body !== null) {
body = JSON.stringify(body);
}
}
//初始化header
if (headers === undefined || headers === null) {
headers = {};
}
//如果在Header中未提供Content-Type, 则默认为表单方式
if (headers['Content-Type'] === undefined) {
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
headers['app-id'] = config.appInfo.id;//当前应用id
headers['app-version'] = config.appInfo.version;//当前应用版本
headers['os-version'] = 'win7';//当前操作系统
headers['device'] = 'pc';//当前设备
headers['client-type'] = 'pc';//当前客户端类型
headers['jwt'] = localStorage.jwt;//从localStorage中获取jwt这个token, 登录成功后就有了
return fetch(url, {
method,
headers,
body
}).then(res => res.json());
};
export const get = (url, headers) => request('GET', url, headers);
export const post = (url, headers, body) => request('POST', url, headers, body);
export const put = (url, headers, body) => request('PUT', url, headers, body);
export const del = (url, headers, body) => request('DELETE', url, headers, body);
export const success = json => json.code === '0';
然后我这么调用的
import * as request from '../../common/request';
...一些代码
request.post(config.api.login, {}, values)
.then((res) => {
console.log('res', res);
});
现在一直报错, 查google之类都没有, 可能这个错误太初级了吧, 域名
你的fetch只写了成功时的回调,没有写错误时的回调,看下是不是请求出错了