A new generation of Ajax Api--fetch based on promise object

Source: Internet
Author: User

Tag: Timeout JSON object character bad ack 16px array. Text throw

1*****************************************************************2#fetch Request using isomorphic-fetch send FETCH request3 4Import fetch from ' Isomorphic-fetch ';//window Native method in ES65 6 Fetch (url,{7Method: "POST",8Headers: {"Content-type": "Text/plain"},9Body: "Firstname=nikhil&favcolor=blue&password=easytoguess"Ten}). Then (function(response) { One     if(Response.Status >= 400) {//determine if the request was successful A         Throw NewError ("Bad response from server!"); -     } -     //response.headers.get (' Content-type ') the     //response.headers.get (' Date ') -     //Response.Status -     //Response.statustext -     //Response.type +     //Response.url -  +     returnResponse.json ();//to turn a promise object into a JSON object A     //. Text (): return string at     //. JSON (): Returns a JSON object -     //. FormData (): Returns a FormData object -     //. BLOB (): Returns a Blob object -     //. ArrayBuffer (): Returns a binary array -}). Then (function(JSON) { -Console.log (JSON);//Execute your code in}).Catch(function(ex) { -Console.log (' request failed ', ex);//Exception Handling to }); +***************************************************************** -  the***************************************************************** *#JSONP Request using fetch-Jsonp sending JSONP requests $ Panax NotoginsengImport Fetchjsonp from ' Fetch-jsonp '; -  the Fetchjsonp (url,{ +JSONP: ' Callback ',//Do not set, default to become callback AJsonpcallback: ' Mycallback ',//Not set, random name generated by default thetimeout:3000,//Not set, default +Data:{a:1},//parameter finally generates a request for Url?a=1&callback=mycallback -}). Then (function(response) { $     returnResponse.json ();//accept the result as a promise object and turn it into a JSON object $}). Then (function(JSON) { -Console.log (JSON);//Execute the code you need -}).Catch(function(ex) { theConsole.log (' parsing failed ', ex);//Exception Handling - })Wuyi  the!!! The data returned by JSONP cannot be pure json, but rather "function name (JSON)"The JS code -*****************************************************************

A new generation of Ajax Api--fetch based on promise object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.