1

I'm having a situation with ajax requests on firefox, i check the type of some requests of firefox on network section of firebug, and the responses are recognized as xml instead of json, getting a incorrect format error, this error not occurs in chrome. I'm using the framework extJS and using the method Ext.Ajax.request to make the requisition.

Here the header of chrome

POST /stricto/crsCronograma/pesquisa?_dc=1451910851660 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 678
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: http://localhost:8080/stricto/?id=desenv_Stricto-SA_7018
Accept-Encoding: gzip, deflate
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: JSESSIONID=95519F96AD70310CE466B2164ED7A08A

and the firefox

Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/stricto/?id=desenv_Stricto-SA_7018
Content-Length: 678
Cookie: JSESSIONID=5DE7F2C586F125FC590548957B486F43
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Here the code to make the request

 request: (function () {

    /**
     * Normaliza os dados de entrada
     * 
     * @return {Object} config
     */
    function processArgs() {
        var defaults = {
            root: 'data',
            params: {}
        };

        if (Sharedev.util.Object.isString(arguments[0]))
            return Ext.apply(defaults, {
                url: arguments[0]
            }, arguments[1]);

        return Ext.apply(defaults, arguments[0] || {});
    }

    /**
     * Normaliza as funções de success ou failure
     */
    function processCallbacks(config) {
        var onResponseSuccess = config.success;
        var onResponseFailure = config.failure;

        return Ext.apply(config, {
            failure: Sharedev.util.Ajax.onRequestFailure,
            success: Sharedev.util.Ajax.onRequestSuccess,
            onResponseSuccess: onResponseSuccess || Sharedev.util.Ajax.onResponseSuccess,
            onResponseFailure: onResponseFailure || Sharedev.util.Ajax.onResponseFailure
        });
    }

    /**
     * Processa os dados do record
     */
    function processRecord(config) {
        var record;

        if (!(record = config.record || config.data))
            return config;

        delete config.data;
        delete config.record;

        if (record instanceof Ext.data.Model) {
            config.model = record.self;
            Ext.applyIf(config.params, Sharedev.util.Object.addPrefix(record.getData(), Sharedev.util.Class.getSimpleName(config.model, true)));
        } else {
            throw new Sharedev.util.Exception.IllegalArgument('A propriedade "record" esta não é uma instancia de um modelo');
        }

        return config;
    }

    function processModel(config) {
        if (!config.model)
            return;

        var Model = config.model && Sharedev.util.Record.getModel(config.model);

        if (!config.url && Model) {
            var simplename = Sharedev.util.Class.getSimpleName(Model, true);
            config.url = './' + simplename + '/' + Sharedev.data.Proxy.prototype.apiMethod.read;
        }

        Ext.applyIf(config.params, {
            map: Sharedev.util.Ajax.initMapProperty(Model, true)
        });

        return config;
    }

    function processMask(config) {
        if (!config.mask)
            return;

        var callback = config.callback;
        var msg, target;

        if (config.mask.isComponent)
            target = config.mask;
        else {
            target = config.mask.target;
            msg = config.mask.msg;
        }

        Sharedev.util.LoadMask.show(target, msg);
        delete config.mask;

        return Ext.apply(config, {
            $maskTarget: target,
            $callback: callback,
            callback: function (config) {
                Sharedev.util.LoadMask.hide(config.$maskTarget);
                if (config.$callback)
                    config.$callback.apply(this, arguments);
            },
        });
    }

    function processFilters(config) {

        if (!config.filters)
            return;

        Ext.applyIf(config.params, {
            filter: Sharedev.util.Ajax.initFilterProperty(config.filters)
        });

        delete config.filters;
        return config;
    }

    return function () {
        var config = processArgs.apply(this, arguments);
        processCallbacks(config);
        processRecord(config);
        processModel(config);
        processFilters(config);
        processMask(config);

        Sharedev.util.Ajax.format(config.params);
        return Ext.Ajax.request(config);
    };

I'm use java on server and the header contntettype is being set in this method

public static void includeJson(Result result, String message) {
    if (result.included().containsKey("message")) {
        return; // so coloca a mensagem uma vez, pois se colocar mais de uma, ira concatenar erroneamente
    }
    result.include("message", message);// adicionado como include somente para verificar se ja foi respondido ou nao

    String contenttype = "application/json";
    if (result.included().containsKey("content-type")) {
        contenttype = (String) result.included().get("content-type");
    }

    result.use(Results.http()).body(message).addHeader("content-type", contenttype);
}
Juliano Grams
  • 525
  • 1
  • 5
  • 17

1 Answers1

1

Firefox is sending a request as specified by you. You did not specify which response type you accept, and Firefox uses the default one that is set in about:config at network.http.accept.default. That default says that Firefox prefers XML over JSON (text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8). The server is then happily serving that type if he can, as per request of your browser.

Chrome, on the other hand, does not define what type it prefers (*/*), while IE tells the server that it prefers JSON. So while your server does serve JSON to Chrome, .NET by default would serve XML to Chrome (and Firefox, sending JSON to IE only).

Long story short, what you want to do is that you explicitly define the return type you can accept:

Ext.Ajax.request({
    ...
    headers:{
        Accept:'application/json'
    }
});

You will see that once you set the Accept header, you are magically served with JSON, provided that the server is correctly recognizing the request type and is able to serve JSON.

Alexander
  • 19,906
  • 19
  • 75
  • 162