0

The third party service I'm using is very particular. I'm trying to set the accepts header to

Accept: application/json

But jquery 1.4.4 won't send just that. It keeps sending a variation, i.e.:

Accept: */*, application/json

I've tried a few variations, i.e. the one below. How can I get it to send just 'Accept: application/json' in the accepts header?

jQuery.ajax({
    url: "/rest/cms.country",
    beforeSend: function (xhr) { xhr.setRequestHeader('Accept', 'application/json'); },
    contentType: "application/json; charset=utf-8",
    type: 'get',
    success: function () {
        console.log('success');
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log('error' + errorThrown);
    }
});
4

1 回答 1

0

According to the W3C spec, setting the request header is supposed to append to any existing value, so it's already '*/*' and you setting it before the request appends 'application/json'.

Also, according to this the spec now requires '*/*' as an accept value, so your server is violating the spec (I know not your issue here).

What you want is the browser to clear out the existing value of '*/*' before setting yours, so maybe call:

setRequestHeader('Accept', '')

before your AJAX request? This is going to be browser-dependent so you're probably in for some IE fun.

于 2012-11-29T23:01:13.997 回答