IIS7 Rewrite
IIS7 Rewrite

Reputation: 799

dojo/request/xhr returning xml instead of json

I'm using a simple dojo xhr request:

require(["dojo/query", "dojo/on", "dojo/dom-style", "dojo/request/xhr", "dojo/domReady!"],
            function (query, on, domStyle, xhr) {

                xhr("api/products", {
                    handleAs: 'json'
                }).then(function (data) {
                    console.log('GOT DATA FROM DOJO XHR', data);
                }, function (err) {
                    console.log('ERROR FROM DOJO XHR', err);
                });
            }
        );

This works fine, but the data returned is as XML not JSON.

However, the same call in jQuery returns the data in JSON.

When I look at the headers, for the jQuery call it shows: Content-Type application/json; charset=utf-8, but for the dojo call it shows: Content-Type application/xml; charset=utf-8

I also added:

headers: { "Content-Type": "application/json; charset=uft-8" }

to the xhr parameters, but still no luck, it still returns xml.

What gives? How do you tell dojo xhr to handle it as json? I'm using dojo 1.8.3.

Upvotes: 2

Views: 1404

Answers (3)

iTolik
iTolik

Reputation: 109

Fixing server side works, but this is a band-aid solution. Server responds correctly to what it sees in the Accept header. Even if in Dojo xhr call you specify 'application/json', for some reason Firefox replaces it with 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8' or something similar. As a result .NET sends back XML instead of JSON. Does not happen in other browsers.

I am still looking at how to fix it in a correct way.

Update: I think I have an answer, but not sure why it fixes it. If I set headers value in xhr request like the following, then everything works in Firefox:

headers: { 'Content-Type': 'application/json; charset=utf-8', 'Accept': 'application/json' }

If I use double-quotes, then these headers are not transmitted to the server and XML is returned instead.

Upvotes: 1

Lucian Depold
Lucian Depold

Reputation: 2017

the server doesnt behvae like that by itself. check using firebug what dojo and jquery are requesting when they do a xhr. there has to be a param that tells the server that it is dojo or jquery. change that parameter.

dojo and jquery are the same, they are based on js and they both use xhr. please consider posting the exact request information for both.

Upvotes: 1

IIS7 Rewrite
IIS7 Rewrite

Reputation: 799

Ok, found the problem of why it's happening, but not the root cause.

I'm using the web api with asp.net mvc4 for the json service. It turns out somehow that for dojo the service is returning xml but for jQuery it returns json.

So, if it interests anyone else, how I fixed it, is that in WebApiConfig I removed xml as a supported return type:

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

Since I"m only interested in JSON, this is ok for me, but if you need to support both, then you may have to look deeper.

So, to summarize, the issue is not really a dojo xhr issue, i.e, not a client issue, it's a server issue not handling the request properly.

Hope it helps anybody else.

Upvotes: 0

Related Questions