在IE的请求中设置AJAX内容类型标头

Kod*_*hor 5 ajax rest jquery internet-explorer content-type

从Internet Explorer发送跨域jquery ajax http请求时,是否可以将http内容类型请求标头设置为'application/json'?

我们正在尝试点击REST WCF服务,该服务在格式化响应时解释请求标头中的内容类型.现在,无论我们在请求标头中放入什么,它总是以XML格式返回数据.

我们尝试使用jquery.ieco​​rs.js插件扩展了jquery ajax调用以使用XDomainRequest对象,但仍然忽略了我们的jquery ajax调用中设置的内容类型.

这是我们的ajax调用的样子:

makeGETRequest: function (requestUrl) {
    return $.ajax({
        type: "GET",
        url: requestUrl,
        contentType: 'application/json',
        dataType:'json',
        cache: false
    });
}
Run Code Online (Sandbox Code Playgroud)

sla*_*pon 10

只需将content-type作为参数之一传递给.ajax方法:

var retval = jQuery.ajax({
    type:'post',
    url: url,
    contentType: 'application/json',
    data: JSON.stringify(data)
});
Run Code Online (Sandbox Code Playgroud)