Elixor
  • Elixor
  • Setup
  • Making a GET request
    • Why write a separate service file
    • Type-checking the response
    • Reading the full response
  • Sending data to the server
    • Adding headers
    • Making a POST request
    • Making a DELETE request
    • Making a PUT request
  • Error handling
    • Getting error details
    • retry()
  • Observables and operators
  • Requesting non-JSON data
  • Advanced Usage
    • Configuring the request
    • Debouncing requests
    • Intercepting requests and responses
    • Listening to progress events
  • Security: XSRF Protection
Powered by GitBook
On this page

Was this helpful?

Making a GET request

Applications often request JSON data from the server. For example, the app might need a configuration file on the server. The ConfigService fetches this file with a get() method.

config.service.ts
import { elixor } from 'elixor';

const url = 'https://jsonplaceholder.typicode.com/posts';
export const getConfig = () => {
  return elixor.get(url);
}

A component, such as ConfigComponent,and calls the getConfig service method.

Config.tsx
import * as React from 'react';
import { getConfig } from './services/config.service';

class Config extends React.Component {

  componentDidMount() {
    getConfig()
      .subscribe(r => {
        console.log(r);
      });
  }
}

export default Config;

Because the service method returns an Observable of configuration data, the component subscribes to the method's return value.

PreviousSetupNextWhy write a separate service file

Last updated 6 years ago

Was this helpful?