Skip to content

Instantly share code, notes, and snippets.

@leohxj
Created August 9, 2017 10:56
Show Gist options
  • Save leohxj/cc08b65863ab2645260e4c7de4651e96 to your computer and use it in GitHub Desktop.
Save leohxj/cc08b65863ab2645260e4c7de4651e96 to your computer and use it in GitHub Desktop.
corsFunc(params) {
const headers = new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
});
return fetch('xxxxxx', {
method: 'post',
headers,
mode: 'cors',
credentials: 'include',
body: JSON.stringify(params),
}).then((response) => {
// 处理网络错误
if (response.status >= 200 && response.status < 300) {
return response.json();
}
const error = new Error(response.statusText);
error.response = response;
error.type = 'statusError';
throw error;
}).then((data) => {
// 对接口数据的
const { success, content } = data;
const result = success;
if (result) {
return content;
}
const error = new Error('Not the expected results');
error.type = 'dataError';
error.response = data;
throw error;
}).catch((e) => {
throw e;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment