Search code examples
javascriptajaxsame-origin-policy

Opening a new window that needs to make ajax calls to another domain


Background We have two web applications hosted on different sub-domains. Application 1 is an internal admin system. Application 2 is a helpdesk system.

We can modify the source code of Application 1 but we have no access to modify Application 2.

The Goal To display a link against an order in Application 1 that will open a new window, the URL of which is that of a ticket in Application 2.

The idea being that our staff can see that an order has a helpdesk ticket raised against it and simply needs to click a link on the order to view the ticket and reply to it.

The problem Regardless of how I open the new window (window.open, target="_blank", etc.) the ticket in the new window is unable to make any ajax requests back to the helpdesk system where it is hosted.

The URL of the new window is part of Application 2.

In Google dev tools it tells me "The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "https". Protocols must match." even when I open it using _blank.

If I go to the exact same URL manually everything works... but this doesn't help when I need it to work from the link.

Is there any way to achieve the above?

If not, is there any way I can open a new window that is "detached" from the window that opened it so that same origin policy no longer applies?

Edit 2014-03-28 10:23 I have no access to App2's code at all. I cannot make any changes to App2. Any answer must take this into account.

I am trying to open a new window from my application (App1) where the target URL of that window is a page in App2. That page inside App2 then needs to be able to use ajax to communicate with other areas of App2. This is where the problem lies. Because App1 opened the window the same origin policy is preventing that window from making it's ajax requests.


Solution

  • I suspect that JavaScript on the second (helpdesk) app is trying to access the first app via window.opener (which could lead to the cross-origin error you're seeing) and subsequent JavaScript (fetching stuff via AJAX) is then not getting executed. You can probably narrow things down by setting appropriate breakpoints in the second app.

    If this is the cause and you can't modify the source for the helpdesk app, how about going to a URL in the internal domain that would then redirect to the help desk? The redirect should cause the window.opener property to become null (same as manually typing in the URL).

    Assuming https://admin.mydomain.co.uk and http://helpdesk.mydomain.co.uk, clicking on the "Help Ticket" link would go to a URL in the internal app, e.g. https://admin.mydomain.co.uk/getHelp?ticketId, which would respond with a 301 response and an appropriate Location: http://helpdesk.domain.uk/help/ticketId header taking the user to the actual helpdesk URL.