The following works in all browsers except IE (I'm testing in IE 9).

jQuery.support.cors = true;
...
        $.ajax(
            url + "messages/postMessageReadByPersonEmail",
            {
                crossDomain: true,
                data: {
                    messageId       : messageId,
                    personEmail     : personEmail
                },
                success: function() {
                    alert('marked as read');
                },
                error: function(a,b,c) {
                    alert('failed');
                },
                type: 'post'
            }
        );

I have another function which uses dataType: 'jsonp', but I don't need any data returned on this AJAX call. My last resort will be to return some jibberish wrapped in JSONP just to make it work.

你知道为什么IE搞砸了一个不返回数据的CORS请求吗?

推荐答案

这是一个已知的jQuery bug.jQuery团队"没有在核心中支持这一点的计划,更适合作为插件."(见this comment).

jQuery中有is个插件可以支持这一点,其中can be found here:

EDIT The function $.ajaxTransport registers a transporter factory. A transporter is used internally by $.ajax to perform requests. Therefore, I assume you should be able to call $.ajax as usual. Information on transporters and extending $.ajax can be found here.

Also, a perhaps better version of this plugin can be found here.

Two other notes:

  1. The object XDomainRequest was introduced from IE8 and will not work in versions below.
  2. 来自IE10的CORS将是supported using a normal XMLHttpRequest.

Edit 2: http to https problem

Requests must be targeted to the same scheme as the hosting page

This restriction means that if your AJAX page is at http://example.com, then your target URL must also begin with HTTP. Similarly, if your AJAX page is at https://example.com, then your target URL must also begin with HTTPS.

Source: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

Jquery相关问答推荐

Bootstrap 4 多选下拉菜单

如何在 jQuery 中获取浏览器滚动位置?

从 JQuery.ajax 成功数据中解析 JSON

有没有办法放大 D3 力布局图?

使用 JQuery 更改 :before css Select 器的宽度属性

如何使用 jQuery UI Resizable 仅水平或垂直调整大小?

javascript 正则表达式用于包含至少 8 个字符、1 个数字、1 个大写和 1 个小写的密码

你如何获得文本区域中的光标位置?

如何使用 jquery 更改 onclick 事件?

jQuery .live() 和 .on() 有什么区别

如何在 JavaScript 或 jQuery 中规范化 HTML?

动态创建并提交表单

用 Javascript 加载 jQuery 并使用 jQuery

.apply jQuery 函数是什么?

无法更新数据属性值

使用 jQuery 获取鼠标单击图像的 X/Y 坐标

你能在不影响历史的情况下使用哈希导航吗?

默认情况下如何将tinymce粘贴为纯文本

如何使用 jquery 更改元素类型

Ajax 处理中的无效 JSON 原语