Web Technology Basics

JavaScript on a page at https://app.example.com uses fetch to send a POST request with a body of Content-Type application/json to https://api.example.net/orders. Which of the following correctly describes how CORS behaves in this case?

  1. ACORS also works outside browsers, so without the server's permission even a POST sent from a command-line tool is stopped along the network
  2. BThe browser first sends a preliminary check (a preflight) with the OPTIONS method, and sends the actual POST only if the server returns permission in its headers
  3. CRequests to a different origin are always sent without any check, and the page's JavaScript can read the response even if the server adds nothing to it
Show answer and explanation

Answer: B. The browser first sends a preliminary check (a preflight) with the OPTIONS method, and sends the actual POST only if the server returns permission in its headers

CORS is a mechanism that allows requests to other origins, and the reading of their responses, which the same-origin policy restricts, within the scope the server permits. A POST with Content-Type application/json is a kind of request that a simple form submission cannot produce, so the browser first uses OPTIONS to state the method and headers it will use and asks for permission. If the server's response does not grant permission with headers such as Access-Control-Allow-Origin, the browser does not send the actual request. CORS is a rule enforced by the browser, so the server must protect itself separately with authentication and permission checks.

  • A. CORS is a check performed by browsers and does not stop requests from tools other than browsers. Server-side authentication and permission checks are needed separately.
  • B. Correct. For a POST with an application/json body, permission is confirmed with a preflight before sending.
  • C. Scripts can read responses from other origins only when the server permits it with headers such as Access-Control-Allow-Origin.
Practice this topic Back to topic

Books on Web Technology Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Web Technology Basics questions

  1. Which of the following correctly describes a Service Worker?
  2. For a page with responsive design that also supports smartphones, which of the following is the main purpose of writing <meta name="viewport" content="width=device-width, initial-scale=1"> in <head>?
  3. The response for a "my page" whose content differs for each user is given Cache-Control: private, max-age=60. Which of the following correctly describes the effect of this setting?
  4. When a web server compresses responses with gzip or Brotli before sending them, which of the following generally gains the least reduction in transferred data from compression?
  5. 1.5 MB of text data is compressed with gzip to one third of its size and sent. The bandwidth is 10 Mbps and the RTT is 100 ms, and before the data starts to be sent, the TCP connection, the TLS handshake and the HTTP request take three round trips in total. Assume 1 MB = 10^6 bytes, and ignore compression and decompression time, headers and waiting caused by TCP congestion control. Which of the following is closest to the time until all the data has been received?