Search code examples
javascriptajaxinternet-explorercross-domainxdr

how to specify dataType in XDR request?


i am using XDR for cross domain resource sharing in ie. It works perfectly. I need to know how to specify the return dataType in this. I need to get json as responseText. Here is my code,

        if (window.XDomainRequest&& $.browser.msie && $.browser.version < 10) {
        xdr = new XDomainRequest();
        if (xdr) {
            xdr.onload = function () {
                var customResponse = xdr.responseText;

                }
            };
            xdr.open("get", url);
            xdr.send();
        }

Solution

  • Unfortunately, XDR does not support setRequestHeader (MSDN shows XDR has a really poor abilities), so you can't tell server you want JSON with Accept request header. But you can provide query string parameter that will tell server this info (url += "?format=json"). Of course, server must react on this param, otherwise it will make no sense. If you maintain your server for yourself, it will be easy task. If not, check if your API documentation allows you to request different content-types. If answer is no for both options, you probably should fallback to JSONp instead of XDR.