本文介绍了AJAX在Chrome发送选项而不是GET / POST / PUT / DELETE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,在工作的内部Web应用程序。在IE10的要求做工精细,但在Chrome所有的AJAX请求(其中有很多)使用选项,而不是任何定义方法,我给它发送。从技术上讲我的要求是跨​​域。该网站是服务于本地主机:6120和服务,我正在做的AJAX请求是57124. 这种封闭的jQuery错误定义的问题,而不是一个真正的解决。

我能做些什么来使用正确的HTTP方法,Ajax请求?

编辑:

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

  jQuery.support.cors = TRUE;
 

和每一个AJAX同样内置:

  VAR URL ='的http://本地主机:57124 /我的/ REST /呼叫;
$阿贾克斯({
    网址:网址,
    数据类型:JSON,
    数据:JSON,
    异步:真正的,
    缓存:假的,
    超时:30000,
    标题:{×丽格式:JSON,X-用户名:用户名},
    成功:功能(数据){
        //我的成功的东西
    },
    错误:函数(请求,状态,错误){
        //我的错误的东西
    },
    键入:POST
});
 

解决方案

Chrome是preflighting寻找的头。如果请求是可以接受的,则它将发送实际请求。如果你这样做跨域,你只需要对付它,否则找到一种方法,使该请求不跨域。这就是为什么jQuery的bug已被关闭,不需额外修复。这是由设计。

I am working on an internal web application at work. In IE10 the requests work fine, but in Chrome all the AJAX requests (which there are many) are sent using OPTIONS instead of whatever defined method I give it. Technically my requests are "cross domain." The site is served on localhost:6120 and the service I'm making AJAX requests to is on 57124. This closed jquery bug defines the issue, but not a real fix.

What can I do to use the proper http method in ajax requests?

Edit:

This is in the document load of every page:

jQuery.support.cors = true;

And every AJAX is built similarly:

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 is preflighting the request to look for CORS headers. If the request is acceptable, it will then send the real request. If you're doing this cross-domain, you will simply have to deal with it or else find a way to make the request non-cross-domain. This is why the jQuery bug was closed as won't-fix. This is by design.

这篇关于AJAX在Chrome发送选项而不是GET / POST / PUT / DELETE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:52