Skip to content

如果接口:Failed to fetch错误,如何关闭连接?使用curRequestController.abort();未生效,请指教 #46

Description

@wpfBruce

const abortController = new AbortController();
const signal = abortController.signal;
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('x-client-token', token);
await fetchEventSource(Url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-client-token': ${token},
},
body: JSON.stringify({ chat_detail_id: chat_detail_id }),
signal: signal,
retry: false,
async onopen(response) {
console.log('Open:', response);
if (
response.ok &&
response.headers.get('content-type') === 'text/event-stream'
) {
console.log("everything's good");
} else if (
response.status >= 400 &&
response.status < 500 &&
response.status !== 429
) {
console.log('请求错误');
} else {
console.log('其他错误');
}
},
async onerror(error) {
console.log('Error:', error);
abortController.abort();
},
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions