Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds CORS support (XDomainRequest) for IE7+ still through XMLHttpRequest #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/index.mjs
Expand Up @@ -11,8 +11,10 @@ export default function(url, options) {

request.withCredentials = options.credentials=='include';

request.onload = () => {
resolve(response());
request.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.status check here should be removed. we only care that readyState is 4.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, I leaved this from my described hack in #106 :)

For my personal task, readystate = 4 will be also triggered on CORS errors and for abort operations.
that is status = 0 for readystate = 4

wait

resolve(response());
}
};

request.onerror = reject;
Expand Down
7 changes: 4 additions & 3 deletions test/index.js
Expand Up @@ -20,6 +20,7 @@ describe('unfetch', () => {
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'),
open: jest.fn(),
send: jest.fn(),
readyState: 4,
status: 200,
statusText: 'OK',
responseText: '{"a":"b"}',
Expand Down Expand Up @@ -60,10 +61,10 @@ describe('unfetch', () => {
expect(xhr.send).toHaveBeenCalledWith(null);
});

expect(xhr.onload).toEqual(expect.any(Function));
expect(xhr.onreadystatechange).toEqual(expect.any(Function));
expect(xhr.onerror).toEqual(expect.any(Function));

xhr.onload();
xhr.onreadystatechange();

return p;
});
Expand All @@ -76,7 +77,7 @@ describe('unfetch', () => {
expect(r.headers.get('X-foo')).toEqual('baz');
});

xhr.onload();
xhr.onreadystatechange();

return p;
});
Expand Down