ANGULAR2 Learning Notes (Http requests)

Source: Internet
Author: User

Refer:

Https://angular.cn/docs/ts/latest/guide/server-communication.html

Https://xgrommx.github.io/rx-book/index.html

There is not much difference in concept.

Here are some examples and small differences:

Different places:

1. Upload file is not supported (XHR of the browser can be easily implemented via the Send FormData Ajax upload), Ng2 no

2. Interceptor interception and transformations are not supported for ng1 (you can try to extend it by inheriting the HTTP service)

3. The default combination of RXJS (can also be easily converted back to the familiar Promise)

Remind:

1.XSRF and Ng1 are identical

2.ng2 has a Memory WebAPI service (In-memory Web API Services) that can emulate the backend Web API server

Example:

1.Headers and Params

New Headers ({"MyHeader": "MyValue" }) Headers.append ("Yourheader", "Yourvalue"new  Urlsearchparams ();p arams.set (' myparam ', ' myvalue 'new  requestoptions ({headers:headers, Search:params}); this. Http.get ("/api/products", Options). topromise (). Then ((response) = {    

2.POST

Let BODY = json.stringify ({    "mk200"new Headers ({' Content-type ': ' Application/json '  New  requestoptions ({headers:headers}); this. Http.post ("/api/products", body, Options). topromise (). Then (response) =    {// do something...});

3.get CSV

New requestoptions ({responseType:ResponseContentType.Text}); this. Http.get ("/demo.csv", Options). topromise (). Then ((response) = {    Console.log ( Response.text ());             

4.by Request

New requestoptions ({    method:RequestMethod.Post,    "/api/products",    new Headers ({' Content-type ': ' Application/json ' }),    " mk200 " })}); this. Http.request ( options). topromise (). Then ((response) = {     //dosomething ...});

ANGULAR2 Learning Notes (Http requests)

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.