简体   繁体   English

无法使用jQuery发出CORS请求

[英]unable to make CORS request with Jquery

I'm following this example: 我正在关注以下示例:

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

Here is my code: 这是我的代码:

<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>    

I'm getting the following error: 我收到以下错误:

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.

Try adding: crossDomain: true 尝试添加:crossDomain:true

You can also try: contentType: "jsonp" 您也可以尝试:contentType:“ jsonp”

From the looks of it, it looks like that the server you are trying to communicate with doesn't have CORS enabled.If it has then your domain from where your sending the GET request is not in the server allowed domain white list. 从外观上看,您尝试与之通信的服务器似乎未启用CORS。如果已启用,则发送GET请求的域不在服务器允许的域白名单中。 I don't see any issues with ajax.If you want to know more about CORS I recently have written a detailed post on CORS here . 我看不到ajax有任何问题。如果您想进一步了解CORS,我最近在这里写了一篇有关CORS的详细文章。 Hope that helps 希望能有所帮助

I don't really know what is the back-end language you are using, but you need to use jsonp to make it more secure and cross-domain: true. 我真的不知道您使用的后端语言是什么,但是您需要使用jsonp使其更加安全和跨域:true。
Access-Control-Allow-Origin: * in this case asterisk could be an allowed host Access-Control-Allow-Origin: *在这种情况下,星号可能是允许的主机
Access-Control-Allow-Methods: POST, GET, OPTIONS be sure your method is in the list. Access-Control-Allow-Methods: POST, GET, OPTIONS确保您的方法在列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM