简体   繁体   English

使用SPServices从另一个域上的Sharepoint获取总计列表项

[英]Get total list items from Sharepoint on another domain using SPServices

this is my first post in Stackoverflow. 这是我在Stackoverflow上的第一篇文章。 Hope you guys can help me with my problem. 希望你们能帮助我解决我的问题。 I have a HTML page on domain1.sharepoint.com and I'm trying to get the total list items from a SharePoint on domain2.sharepoint.com using JQuery+SPServices. 我在domain1.sharepoint.com上有一个HTML页面,我正在尝试使用JQuery + SPServices从domain2.sharepoint.com上的SharePoint获取总列表项。

I understand that due to same-origin policy, I need to use XMLHttpRequest (CORS) to get the data I need. 我了解由于同源策略,我需要使用XMLHttpRequest(CORS)来获取所需的数据。 Unfortunately, I'm not sure how to put the XMLHttpRequest into SPServices. 不幸的是,我不确定如何将XMLHttpRequest放入SPServices。 I've tried, and getting 'undefined' error. 我已经尝试过,并且收到“未定义”错误。 I'm pretty new with JavaScript, appreciate all the help. 我对JavaScript非常陌生,感谢所有帮助。

Here's what I've done so far: 到目前为止,这是我所做的:

$.support.cors = true;
function createCORSRequest(method, refURL) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    xhr.open(method, refURL, true);
  } else if (typeof XDomainRequest != "undefined") {
    xhr = new XDomainRequest();
    xhr.open(method, refURL);
  } else {
    xhr = null;
  }
  return xhr;
}

function makeCorsRequest() {
  var refURL = 'http://domain1.sharepoint.com';
  var spURL = refURL;

  var xhr = createCORSRequest('GET', spURL);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }
  xhr.onload = function() {
    $().SPServices({
        operation: "GetListItems",
        webURL: spURL,
        listName: "Message Log",
        viewName: "{70DFBF9A-5266-4D69-AD01-E848301B51BB}",
        async: false,
        CAMLViewFields: "<ViewFields><FieldRef Name='Status' /></ViewFields>",
        completefunc: function (xData, status) {
            $("#countlist").html($(xData.responseXML).find("z\\:row").length);
        }
    });
};

  xhr.onerror = function(e, jqxhr) {
    alert('Woops, there was an error making the request. ' + jqxhr);
  };

  xhr.send();
}

onload = makeCorsRequest;

I think you are doing too much. 我觉得你做得太多了。 This line: 这行:

$.support.cors = true; $ .support.cors = true;

Should be the only thing you need in orde to enable cross domain. 启用跨域功能是您唯一需要的命令。 This tells jQuery to enable cors an it takes care of the rest. 这告诉jQuery启用cors,并负责其余的工作。

Once this is set, can can call SPServices the normal way and insuring you set the 'webURL' option to point to the domain2. 设置好之后,可以正常方式调用SPServices,并确保将“ webURL”选项设置为指向domain2。

Note that although this will work in 'modern' browsers, IE (I think) will prompt the user for permission. 请注意,尽管这将在“现代”浏览器中运行,但IE(我认为)将提示用户进行许可。

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

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