没有'Access-Control-Allow-Origin'标题

时间:2014-12-02 18:53:16

标签: javascript sharepoint azure xmlhttprequest csom

我有一个部署到Azure的MVC应用程序,它是作为SharePoint Online站点的SharePoint应用程序部件(提供程序托管)创建的。在SharePoint网站和Azure Web应用程序中正确设置了所有客户端ID和密钥ID。

应用程序确实从SharePoint站点启动,但JSOM逻辑正在抛出No' Access-Control-Allow-Origin'运行这个简单的JS逻辑时的头例件...



<script type="text/javascript">
    var hostweburl;

    // Load the required SharePoint libraries.
    $(document).ready(function () {

        // Get the URI decoded URLs.
        hostweburl =
            decodeURIComponent(
                getQueryStringParameter("SPHostUrl")
        );

        // The js files are in a URL in the form:
        // web_url/_layouts/15/resource_file
        var scriptbase = hostweburl + "/_layouts/15/";

        // Load the js files and continue to
        // the execOperation function.
        $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js", execOperation);
            }
        );
    });

    // Function to execute basic operations.
    function execOperation() {

        // Continue your program flow here.
        hostweburl =
           decodeURIComponent(
               getQueryStringParameter("SPHostUrl")
       );
        retrieveWebSite(hostweburl);

    }

    // Function to retrieve a query string value.
    // For production purposes you may want to use
    // a library to handle the query string.
    function getQueryStringParameter(paramToRetrieve) {
        var params =
            document.URL.split("?")[1].split("&");
        var strParams = "";
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
        }
    }


    function retrieveWebSite(siteUrl) {
        var clientContext = new SP.ClientContext(siteUrl);
        this.oWebsite = clientContext.get_web();

        clientContext.load(this.oWebsite);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),
            Function.createDelegate(this, this.onQueryFailed)
        );
    }

    function onQuerySucceeded(sender, args) {
        alert('Title: ' + this.oWebsite.get_title() +
            ' Description: ' + this.oWebsite.get_description());
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +
            '\n' + args.get_stackTrace());
    }

</script>
&#13;
&#13;
&#13;

当函数retrieveWebSite尝试获取站点的客户端上下文时,会发生异常...

        var clientContext = new SP.ClientContext(siteUrl);

例外情况如下......

XMLHttpRequest无法加载https://mySharePointSiteName.sharepoint.com/sites/Apps/_api/contextinfo。 No&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上。起源&#39; https://myWebSiteName.azurewebsites.net&#39;因此不允许访问。

我认为整个IFrames都会照顾到这个?

1 个答案:

答案 0 :(得分:2)