【问题标题】:unable to make CORS request with Jquery无法使用 Jquery 发出 CORS 请求
【发布时间】:2014-11-14 14:46:45
【问题描述】:

我正在关注这个例子:

http://www.html5rocks.com/en/tutorials/cors/#toc-cors-from-jquery

这是我的代码:

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
    <script>
    $(document).ready(function(){

        $.getJSON("http://172.28.101.197:3000/tests.json",function(config){

            var create_config_url = "https://act.domain.com/act2/act/createConfigfile?accountid=" + config.accountId + "&configfilename=" + config.name

            alert ("here goes nothing!");
            $.ajax({


                // The 'type' property sets the HTTP method.
                // A value of 'PUT' or 'DELETE' will trigger a preflight request.
                type: 'GET',

                // The URL to make the request to.
                url: create_config_url,

                // The 'contentType' property sets the 'Content-Type' header.
                // The JQuery default for this property is
                // 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
                // a preflight. If you set this value to anything other than
                // application/x-www-form-urlencoded, multipart/form-data, or text/plain,
                // you will trigger a preflight request.
                contentType: 'text/plain',

                xhrFields: {
                // The 'xhrFields' property sets additional fields on the XMLHttpRequest.
                // This can be used to set the 'withCredentials' property.
                // Set the value to 'true' if you'd like to pass cookies to the server.
                // If this is enabled, your server must respond with the header
                // 'Access-Control-Allow-Credentials: true'.
                    withCredentials: false
                },

                headers: {
                // Set any custom headers here.
                // If you set any non-simple headers, your server must include these
                // headers in the 'Access-Control-Allow-Headers' response header.
                },

                success: function() {
                    alert( "I think I created a config" );
                // Here's where you handle a successful response.
                },

                error: function() {

                    alert ("That's an error!");
                // Here's where you handle an error response.
                // Note that if the error was due to a CORS issue,
                // this function will still fire, but there won't be any additional
                // information about the error.
                }
                //$.get(create_config_url,function(result){

            });

        });
});</script>
</body>
</html>    

我收到以下错误:

XMLHttpRequest cannot load https://act.domain.com/act2/act/createConfigfile?accountid=AANA-EJ8SB&configfilename=rabdelaz.netstorage-pm.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

【问题讨论】:

  • 这是服务器的问题,而不是你的 jQuery。
  • 该死的。没有办法解决吗?

标签: jquery cors


【解决方案1】:

尝试添加:crossDomain: true

你也可以试试:contentType: "jsonp"

【讨论】:

  • 具体在哪里?是独立的还是嵌套在其他东西中?
  • 请更具体。例如,您可以在代码 sn-p 处演示您的意思(例如,在使用 jsonp 时,仅使用该内容类型可能是不够的)。
  • 你知道crossDomain: true是干什么用的吗?
【解决方案2】:

从外观上看,您尝试与之通信的服务器似乎没有启用 CORS。如果它启用了,那么您发送 GET 请求的域不在服务器允许的域白名单中.我没有看到 ajax 有任何问题。如果您想了解更多关于 CORS 的信息,我最近在 CORS here 上写了一篇详细的帖子。希望有帮助

【讨论】:

    【解决方案3】:

    我真的不知道您使用的是什么后端语言,但您需要使用 jsonp 使其更安全和跨域:true。
    Access-Control-Allow-Origin: * 在这种情况下,星号可能是允许的主机
    Access-Control-Allow-Methods: POST, GET, OPTIONS 确保您的方法在列表中。

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 2014-02-17
      • 2019-10-21
      • 2015-02-25
      • 1970-01-01
      • 2013-04-08
      • 2011-08-09
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多