Compartilhar via


Cross Origin Request Implementation for Internet Explorer 8

Internet Explorer 8 and below does not support cross origin requests. To enable cross domain requests in IE 8, we have to follow certain workarounds.

IE 8 allows “postMessage” method. We take advantage of this method to communicate between the host page and the cross origin page that is loaded in an IFrame in the host page.

The cross origin page which is loaded in the IFrame, attaches itself to the “onmessage” event and begins listening for incoming messages.

When the cross domain page receives any message, it parses the message and then performs the requested operation. It will then send back response to host page through “postMessage” method.

If there are multiple concurrent requests, the host page needs to figure out the request-response mapping. For this, we declare a global counter in the page called messageIndex. Each message that the host page sends to the IFrame contains certain parameters as properties:

  1. URL – The URL where the request needs to be sent for processing
  2. Type – GET/POST/PUT/DELETE
  3. Content Type – text/json etc.
  4. Payload Data
  5. Message ID – The globally incrementing counter (messageIndex).

The cross domain page would process the request and send back the response which will contain the following properties:

  1. Result
  2. Message ID

At the host page, we again attach to the “onmessage” event and receive and process the data accordingly.

The cross origin page can decide whether to allow request coming from any origin or can check the origin of the incoming request and allow/reject the call.