在IE的请求中设置AJAX内容类型标头 [英] Set AJAX content type header in request from IE

查看:172
本文介绍了在IE的请求中设置AJAX内容类型标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to set the http content-type request header to 'application/json' when sending a cross domain jquery ajax http request from Internet Explorer?

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

We're trying to hit a REST WCF service that interprets the content type from the request header when formatting the response. Right now, no matter what we put in the request header it is always returning the data in XML format.

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

We've tried using the jquery.iecors.js plugin which extends the jquery ajax call to use the XDomainRequest object but that is still ignoring the content-type that is set in our jquery ajax call.

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

Here's what our ajax call looks like:

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


推荐答案

只需传递内容类型作为 .ajax 方法的参数之一:

Just pass the content-type as one of your parameters to the .ajax method:

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

这篇关于在IE的请求中设置AJAX内容类型标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆