JSSDK版本怎么实现fetcher的重写?
后端接口返回的规范和amis的不一样,现在用的 JSSDK 版本 ,要怎么去重写 fetcher 呢?
https://baidu.github.io/amis/docs/getting-started#jssdk
看下这个,可以自定义实现fetcher
amis.embed('#root', page_json, {}, { fetcher: function(params){ var method = params.method; var url = params.url; return new Promise(function (resolve, reject) { fetch(url, { method: method, headers: new Headers({ "Content-Type": "application/json" }) }).then(function(res){ return res.json(); }).then(function(response){ console.log(response); resolve(JSON.stringify(response)); }); }); } });
jssdk报错Error: Response is empty!。实际上接口已经调用成功
已经解决。这个错误提示有点坑。 改为
resolve({ "data": response });
let amisScoped = amis.embed('#root', amisJSON, {}, {
fetcher: function (params) {
return new Promise(function (resolve, reject) {
// console.log((params.data));
let esse_access_token = sessionStorage.getItem('access_token');
let content = esse_access_token ? JSON.parse(access_token).content : ''
let headers = params.headers;
if (content) {
headers['Authorization'] = 'Bearer ' + content
}
fetch(params.url, {
method: params.method,
body: params.data,
headers: headers
}).then((response) => {
if (response.ok) {
response.json().then(json => {
var data = json.data;
if (data) {
resolve({ data });
} else {
reject(new Error("ERROR in response.json().data"))
}
});
}
}).catch(error => {
reject(new Error(error))
});
});
}
});
尝试这样写可以获取到 数据,但是选中记录做修改的时候会报"FatchFailed"。不能覆盖所有场景