jeudi 8 décembre 2016

Reliable way to detect a JSONP request on the server side?

I have a web application that can serve some requests to the same URL either as HTML or as JSON. So far I relied on the Accept HTTP header to determine what to return. Now there is a new requirement to be able to serve the JSON as a JSONP response if a specific URL parameter exists. Unfortunately most browsers request the src attribute of a <script> tag with an Accept header of */*.

At the moment i see two options around this and both have some drawbacks:

  1. When the Accept header is */*, check for a callback parameter and if that parameter exists, serve JSONP. This might lead to the wrong content type when the callback parameter is used for different purposes.
  2. Do not rely on the Accept header any more and use different URL paths for different content types. This might break backwards compatibility of applications that consume the JSON or lead to duplicate URLs.

Is there any other way to detect JSONP requests?

Aucun commentaire:

Enregistrer un commentaire