jquery - Chrome 中的 AJAX 发送选项而不是 GET/POST/PUT/DELETE?

标签 jquery ajax cross-domain

我正在工作中开发内部 Web 应用程序。在 IE10 中,请求工作正常,但在 Chrome 中,所有 AJAX 请求(有很多)都是使用 OPTIONS 发送的,而不是我给它的任何定义的方法。从技术上讲,我的请求是“跨域”。该站点在 localhost:6120 上提供服务,我向其发出 AJAX 请求的服务位于 57124 上。This closed jquery bug定义了问题,但不是真正的解决方案。

如何在 ajax 请求中使用正确的 http 方法?

编辑:

这是在每个页面的文档加载中:

jQuery.support.cors = true;

每个 AJAX 的构建方式都类似:

var url = 'http://localhost:57124/My/Rest/Call';
$.ajax({
    url: url,
    dataType: "json",
    data: json,
    async: true,
    cache: false,
    timeout: 30000,
    headers: { "x-li-format": "json", "X-UserName": userName },
    success: function (data) {
        // my success stuff
    },
    error: function (request, status, error) {
        // my error stuff
    },
    type: "POST"
});

最佳答案

Chrome 正在预检请求以查找 CORS header 。如果请求可以接受,它将发送真正的请求。如果你要做这个跨域,你只需要处理它,或者找到一种方法使请求非跨域。这就是 jQuery 错误因无法修复而被关闭的原因。这是设计使然。

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data. In particular, a request is preflighted if:

  • It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.
  • It sets custom headers in the request (e.g. the request uses a header such as X-PINGOTHER)

关于jquery - Chrome 中的 AJAX 发送选项而不是 GET/POST/PUT/DELETE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45181864/

相关文章:

javascript - 如何使用 Jquery 在 AJAX 请求后替换元素的文本

javascript - 将 JSON 响应转换为 map

javascript - 无法跨域获取格式错误的 JSON

google-analytics - Google Analytics Auto Linker 跨域链接的替代方案

ajax - 使用 AJAX 连接 Facebook HTML5 点赞和评论元素

javascript - OpenLayers => 特征为空

ajax - 如何在redux中发出AJAX请求

iframe - 谷歌分析显示来自(脚本加载)第 3 方 iFrame 的推荐流量

php - 确认密码字段

javascript - Google Maps Api v3,使用地点库时的动态缩放级别