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:
- URL – The URL where the request needs to be sent for processing
- Type – GET/POST/PUT/DELETE
- Content Type – text/json etc.
- Payload Data
- 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:
- 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.