No 'Access-Control-Allow-Origin' header is present on the requested resource

二次信任 提交于 2019-12-20 03:23:44

问题


    <!DOCTYPE html>
        <html>
            <head>
                <meta charset="utf-8" />
                <meta name="viewport" content="width=device-width, initial-scale=1" />
                <title>webservices example </title>
                <link rel="stylesheet" href="jquery.mobile-1.1.0.css" />
                <link rel="stylesheet" href="my.css" />
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
                <script type="text/javascript">
                    $(document).ready(function() {
                            $.ajax({
                                type:"GET",
                            url:"http://shoperola.com/Restaurant/foodysrest/sample",
                                 crossDomain: true,
                                 contentType: "application/json; charset=utf-8",

                                 dataType: "json",
xhrFields: {

       withCredentials: true
  },
             beforeSend: function (request) {

request.setRequestHeader("Access-Control-Allow-Origin: ", "*")
},
                                 success: function(data) {
                                                alert(data);
                                                },
                                 error: function(data) {
                                alert("Error in Processing-----" + data.status);
                                          }
                                });
                        });

                </script>
            </head>
        </html>

" XMLHttpRequest cannot load http://shoperola.com/Restaurant/foodysrest/sample?{}. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://:8088' is therefore not allowed access".


回答1:


you are trying to perform XMLHttpRequest(AJAX) to another domain.

This is restricted due to security reasons.

for more details see: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing




回答2:


you are missing semicolon on line:

request.setRequestHeader("Access-Control-Allow-Origin: ", "*")



来源:https://stackoverflow.com/questions/20973622/no-access-control-allow-origin-header-is-present-on-the-requested-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!