Q: What is the difference between JSONP and CORS for cross-origin requests?
A: The key differences are:
- Compatibility: JSONP works with older browsers that do not support CORS, whereas CORS is a standardised mechanism supported by modern browsers.
- Request Type: JSONP is not a traditional AJAX request; it uses a
<script>tag. CORS is a mechanism built into the standard HTTP request-response model. - Initiator: JSONP requests are initiated by the client's browser. CORS requests are initiated by the server's response headers.
- Mechanism: JSONP works by specifying a callback function in the request URL. CORS works by setting HTTP headers on the server response.
- HTTP Method Support: JSONP only supports
GETrequests, while CORS supports all HTTP methods (GET, POST, PUT, DELETE, etc.).