Skip to content

Commit 31f83a5

Browse files
committed
network draft
1 parent 68d1ac1 commit 31f83a5

File tree

13 files changed

+218
-27222
lines changed

13 files changed

+218
-27222
lines changed

7-network/10-xmlhttprequest/article.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,23 @@ function upload(file) {
445445
</script>
446446
```
447447

448+
## Cross-origin requests
449+
450+
`XMLHttpRequest` can make cross-domain requests, using the same CORS policy as `fetch`.
451+
452+
Just like `fetch`, it doesn't send cookies and HTTP-authorization to another origin by default. To enable them, set `xhr.withCredentials` to `true`:
453+
454+
```js
455+
let xhr = new XMLHttpRequest();
456+
*!*
457+
xhr.withCredentials = true;
458+
*/!*
459+
s
460+
xhr.open('POST', 'http://anywhere.com/request');
461+
...
462+
```
463+
464+
448465
## Summary
449466

450467
Typical code of the GET-request with `XMLHttpRequest`:

7-network/2-fetch-crossorigin/article.md

Lines changed: 169 additions & 248 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const Koa = require('koa');
2+
const app = new Koa();
3+
4+
const Router = require('koa-router');
5+
6+
let router = new Router();
7+
8+
router.get('/script', async (ctx) => {
9+
let callback = ctx.query.callback;
10+
11+
if (!callback) {
12+
ctx.throw(400, 'Callback required!');
13+
}
14+
15+
ctx.type = 'application/javascript';
16+
ctx.body = `${callback}({
17+
temperature: 25,
18+
humidity: 78
19+
})`;
20+
});
21+
22+
app
23+
.use(router.routes())
24+
.use(router.allowedMethods());
25+
26+
27+
if (!module.parent) {
28+
http.createServer(app.callback()).listen(8080);
29+
} else {
30+
exports.accept = app.callback();
31+
}

7-network/2-fetch-crossorigin/progress.view/long.txt

Lines changed: 0 additions & 26973 deletions
This file was deleted.
Loading
-39.6 KB
Loading
7.44 KB
Loading
18.4 KB
Loading

7-network/3-fetch-api/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let promise = fetch(url, {
1616
body: undefined // string, FormData, Blob, BufferSource, or URLSearchParams
1717
referrer: "about:client", // "" for no-referrer, or an url from the current origin
1818
referrerPolicy: "no-referrer-when-downgrade", // no-referrer, origin, same-origin...
19-
mode: "cors", // same-origin, no-cors, navigate, or websocket
19+
mode: "cors", // same-origin, no-cors
2020
credentials: "same-origin", // omit, include
2121
cache: "default", // no-store, reload, no-cache, force-cache, or only-if-cached
2222
redirect: "follow", // manual, error

7-network/6-url/url-object.png

-26 Bytes
Loading

7-network/6-url/[email protected]

-60 Bytes
Loading

figures.sketch

1.88 KB
Binary file not shown.

0 commit comments

Comments
 (0)