简体   繁体   English

没有“Access-Control-Allow-Origin”标头

[英]No 'Access-Control-Allow-Origin' header

I have an MVC application deployed to Azure which was created as a SharePoint App Part (Provider-Hosted) for a SharePoint Online site.我有一个部署到 Azure 的 MVC 应用程序,它是作为 SharePoint Online 网站的 SharePoint 应用程序部件(提供商托管)创建的。 All Client ID and Secret ID are set correctly within SharePoint site and the Azure web Application.在 SharePoint 站点和 Azure Web 应用程序中正确设置了所有客户端 ID 和机密 ID。

The App does spin up from the SharePoint site but the JSOM logic is throwing a No 'Access-Control-Allow-Origin' header exception when it runs this simple JS logic...该应用程序确实从 SharePoint 站点启动,但 JSOM 逻辑在运行这个简单的 JS 逻辑时抛出了 No 'Access-Control-Allow-Origin' 标头异常......

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

The exception occurs in the function retrieveWebSite when it attempts to obtain the Client Context for the site...当它尝试获取站点的客户端上下文时,函数retrieveWebSite 中发生异常...

        var clientContext = new SP.ClientContext(siteUrl);

The exception is as follows ...例外情况如下...

XMLHttpRequest cannot load https://mySharePointSiteName.sharepoint.com/sites/Apps/_api/contextinfo . XMLHttpRequest 无法加载https://mySharePointSiteName.sharepoint.com/sites/Apps/_api/contextinfo No 'Access-Control-Allow-Origin' header is present on the requested resource.请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' https://myWebSiteName.azurewebsites.net ' is therefore not allowed access.因此,不允许访问源“ https://myWebSiteName.azurewebsites.net ”。

I thought the whole IFrames stuff takes care of this?我认为整个 IFrames 的东西都解决了这个问题?

You need to use the SP.RequestExecutor like so:您需要像这样使用 SP.RequestExecutor:

http://blogs.msdn.com/b/officeapps/archive/2012/11/29/solving-cross-domain-problems-in-apps-for-sharepoint.aspx http://blogs.msdn.com/b/officeapps/archive/2012/11/29/solving-cross-domain-problems-in-apps-for-sharepoint.aspx

-Edit -编辑

Not sure anyone uses this method anymore but here's a new link:不确定是否有人再使用此方法,但这里有一个新链接:

https://docs.microsoft.com/en-us/archive/blogs/officeapps/solving-cross-domain-problems-in-apps-for-sharepoint https://docs.microsoft.com/en-us/archive/blogs/officeapps/solving-cross-domain-problems-in-apps-for-sharepoint

and here's the sample snip from the blog:这是博客中的示例片段:

 // Load the cross-domain library. 
$(document).ready(function () { 
  var hostweburl; 
  var appweburl; 
  
  //Get the URI decoded URLs. 
  hostweburl = decodeURIComponent( getQueryStringParameter("SPHostUrl") ); 
  appweburl = decodeURIComponent( getQueryStringParameter("SPAppWebUrl") ); 
  
  // Load the .js files using jQuery's getScript function. 
  $.getScript(
    hostweburl + "/_layouts/15/SP.RequestExecutor.js",
    continueExecution
  );
  
  // After the cross-domain library is loaded, execution 
  // continues to this function. 
  function continueExecution() { 
    var executor; 
    
    // Initialize your RequestExecutor object. 
    executor = new SP.RequestExecutor(appweburl); 
    // You can issue requests here using the executeAsync method 
    // of the RequestExecutor object.
  } 
  
  // Function to retrieve a query string value.  
  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];
    }
  }
});

Have a great day!祝你有美好的一天!

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

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