AJAX在Chrome发送选项而不是GET / POST / PUT / DELETE? [英] AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?

查看:321
本文介绍了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寻找的 CORS 头。如果请求是可以接受的,则它将发送实际请求。如果你这样做跨域,你只需要对付它,否则找到一种方法,使该请求不跨域。这就是为什么jQuery的bug已被关闭,不需额外修复。这是由设计。

  

不同于简单的要求(如上所述),preflighted请求第一   送由OPTIONS方法的HTTP请求来对资源   其他域,以确定实际的请求是否是安全的   发送。跨站点请求是因为它们可能pflighted这样$ P $   具有影响到的用户数据。特别是,一个要求是   preflighted如果:

     
      
  • 在它使用的方法不是GET,HEAD或POST等。此外,如果POST用于与内容类型比其它发送请求数据   应用程序/ x-WWW的形式urlen codeD,多部分/格式数据或text / plain的,   例如如果POST请求使用发送XML有效载荷到服务器   应用程序/ xml或text / xml的,则要求是pflighted $ P $。
  •   
  • 据请求设置自定义标题(如请求使用如X-PINGOTHER一个头)
  •   

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.

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)

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

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