@@ -613,6 +613,122 @@ Access-Control-Allow-Credentials: true
613613
614614CORS机制与JSONP模式的使用目的相同,而且更强大。JSONP只支持GET请求,CORS可以支持所有类型的HTTP请求。在发生错误的情况下,CORS可以得到更详细的错误信息,部署更有针对性的错误处理代码。JSONP的优势在于可以用于老式浏览器,以及可以向不支持CORS的网站请求数据。
615615
616+ ## Fetch API
617+
618+ Fetch API是一种新兴的规范,用来操作浏览器发出的网络请求,目的是在将来取代XMLHttpRequest。它的作用与XMLHttpRequest类似,但是返回的是Promise对象,因此API大大简化,并且避免了嵌套的回调函数。
619+
620+ 下面是一个XMLHttpRequest对象发出Ajax请求的常规例子。
621+
622+ ``` javascript
623+
624+ function reqListener () {
625+ var data = JSON .parse (this .responseText );
626+ console .log (data);
627+ }
628+
629+ function reqError (err ) {
630+ console .log (' Fetch Error :-S' , err);
631+ }
632+
633+ var oReq = new XMLHttpRequest ();
634+ oReq .onload = reqListener;
635+ oReq .onerror = reqError;
636+ oReq .open (' get' , ' ./api/some.json' , true );
637+ oReq .send ();
638+
639+ ```
640+
641+ 上面的例子用Fetch实现如下。
642+
643+ ``` javascript
644+
645+ fetch (' ./api/some.json' )
646+ .then (function (response ) {
647+ if (response .status !== 200 ) {
648+ console .log (' 请求失败,状态码:' + response .status );
649+ return ;
650+ }
651+ response .json ().then (function (data ) {
652+ console .log (data);
653+ });
654+ }).catch (function (err ) {
655+ console .log (' 出错:' , err);
656+ });
657+
658+ ```
659+
660+ 上面代码中,因为HTTP请求返回的response对象是一个Stream对象,使用json方法转为JSON格式,但是json方法返回的是一个Promise对象。
661+
662+ response对象除了json方法,还包含了HTTP回应的元数据。
663+
664+ ``` javascript
665+
666+ fetch (' users.json' ).then (function (response ) {
667+ console .log (response .headers .get (' Content-Type' ));
668+ console .log (response .headers .get (' Date' ));
669+
670+ console .log (response .status );
671+ console .log (response .statusText );
672+ console .log (response .type );
673+ console .log (response .url );
674+ });
675+
676+ ```
677+
678+ 上面代码中,response.type表示HTTP回应的类型,它有以下三个值。
679+
680+ - basic:正常的同域请求
681+ - cors:CORS机制下的跨域请求
682+ - opaque:非CORS机制下的跨域请求,这时无法读取返回的数据,也无法判断是否请求成功
683+
684+ 如果需要在CORS机制下发出跨域请求,需要指明状态。
685+
686+ ``` javascript
687+
688+ fetch (' http://some-site.com/cors-enabled/some.json' , {mode: ' cors' })
689+ .then (function (response ) {
690+ return response .text ();
691+ })
692+ .then (function (text ) {
693+ console .log (' Request successful' , text);
694+ })
695+ .catch (function (error ) {
696+ log (' Request failed' , error)
697+ });
698+
699+ ```
700+
701+ 除了指定模式,fetch方法的第二个参数还可以用来配置其他值,比如指定cookie连同HTTP请求一起发出。
702+
703+ ``` javascript
704+
705+ fetch (url, {
706+ credentials: ' include'
707+ })
708+
709+ ```
710+
711+ 发出POST请求的写法如下。
712+
713+ ``` javascript
714+
715+ fetch (url, {
716+ method: ' post' ,
717+ headers: {
718+ " Content-type" : " application/x-www-form-urlencoded; charset=UTF-8"
719+ },
720+ body: ' foo=bar&lorem=ipsum'
721+ })
722+ .then (json)
723+ .then (function (data ) {
724+ console .log (' Request succeeded with JSON response' , data);
725+ })
726+ .catch (function (error ) {
727+ console .log (' Request failed' , error);
728+ });
729+
730+ ```
731+
616732## 参考链接
617733
618734- MDN, [ Using XMLHttpRequest] ( https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest )
@@ -621,3 +737,4 @@ CORS机制与JSONP模式的使用目的相同,而且更强大。JSONP只支持
621737- Matt West, [ Uploading Files with AJAX] ( http://blog.teamtreehouse.com/uploading-files-ajax )
622738- Monsur Hossain, [ Using CORS] ( http://www.html5rocks.com/en/tutorials/cors/ )
623739- MDN, [ HTTP access control (CORS)] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS )
740+ - Matt Gaunt, [ Introduction to fetch()] ( http://updates.html5rocks.com/2015/03/introduction-to-fetch )
0 commit comments