CORS 无法在 Chrome 上运行

CORS not working on Chrome(CORS 无法在 Chrome 上运行)
本文介绍了CORS 无法在 Chrome 上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经在服务器上设置了跨域资源共享(Jetty 使用 CrossOriginFilter),它在 IE8 和 Firefox 上完美运行.在 Chrome 上,它只是......没有.

I've set up Cross-Origin Resource Sharing on a server (Jetty using the CrossOriginFilter) and it works perfectly on IE8 and Firefox. On Chrome, it just ... doesn't.

  $.ajax({ url : crossOriginURL,
    type : "GET",
    error : function(req, message) {
        alert(message);
    },
    dataType :  "json" } );

错误函数被调用,并带有有用的消息错误".它似乎正在发出请求,但没有您期望的任何标头.如果 URL 来自同一来源,则可以正常工作.

The error function is invoked, with the helpful message "error". It seems to be making the request, but without any of the headers you'd expect. If the URL is from the same origin, it works fine.

推荐答案

最后对我有用的是 xhr.setRequestHeader('Content-Type', 'text/plain');

服务器需要添加Access-Control-Allow-Headers: Content-Type来避免这个问题.

The server needs to add Access-Control-Allow-Headers: Content-Type to avoid this problem.

十年后,我又回到了我自己的问题上.我不知道这是好事还是坏事.

I am coming back to my own question a decade later. I don’t know if this is a good thing or a terrible thing.

这篇关于CORS 无法在 Chrome 上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)