Knowledgebase

Fetching Data and Working With APIs Print

  • webdevelopment, performance, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Communicating with a server.

WHAT A REQUEST INVOLVES

An address A method, indicating intent Headers Sometimes a body

WHAT TO ALWAYS CHECK

The response status, before using the content.

WHY

A failed request still returns a response, and treating it as data produces confusing errors.

WHAT FORMAT IS USUALLY USED

Structured text, parsed into objects.

WHAT TO VALIDATE

That the response has the shape you expect.

WHY

External data changes, and assuming its structure produces failures in production.

WHAT TO HANDLE

Network failure Unexpected status codes Responses in the wrong format Slow responses

WHAT TO NEVER PUT IN BROWSER CODE

Secrets of any kind.

WHY

Everything in the browser is visible to the user.

WHAT THAT MEANS

Any key in your JavaScript is public.

WHAT TO DO INSTEAD

Route requests requiring secrets through your own server.

WHAT TO BE AWARE OF

Cross-origin restrictions, which prevent a page reading responses from other sites unless permitted.

WHY THEY EXIST

To prevent one site reading your data from another.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot