Visitar URL original
Adds CORS support (XDomainRequest) for IE7+ still through XMLHttpRequest by 3F · Pull Request #107 · developit/unfetch · GitHub
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adds CORS support (XDomainRequest) for IE7+ still through XMLHttpRequest
Details in #106
  • Loading branch information
3F committed Jan 8, 2019
commit 7d1854198d9806ed2aea11bc555e4d17f54b54bd
6 changes: 4 additions & 2 deletions src/index.mjs
Original file line number Diff line number Diff line change
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
Copy Markdown
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
Copy Markdown
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
Original file line number Diff line number Diff line change
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