解决SHAREJPOINT 跨域问题
目前仅支持IE7/8不支持IE11和谷歌
对于跨域情况,目前找到如果jquery是get获取方式,可以配置web.config相关属性,具体powershell命令如下:
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0 $localFarm = Get-SPFarm $webapp = Get-SPWebApplication "http://wtcsps98" # Remove old web.config modifications of MyAuthenticationProvider
$oldMods = @();
$webapp.WebConfigModifications | ? { $_.Owner -eq "CrossSiteScripting" } | % {
$oldMods = $oldMods + $_
} $oldMods | % {
$webapp.WebConfigModifications.Remove($_)
} # update the Web Application and apply all existing web.config modifications - this executes the "remove" actions from above
$webapp.Update()
[Microsoft.SharePoint.Administration.SPWebService]::ContentService.ApplyWebConfigModifications() #Wait until web.config modifications finished by timer job
while( (Get-SPTimerJob | ? { $_.Name -eq "job-webconfig-modification"}) -ne $null ) {
Write-Host "." -NoNewline
Start-Sleep 1
} # New web.config modifications for MyAuthenticationProvider
$myModification1 = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$myModification1.Path = "configuration/system.webServer/httpProtocol/customHeaders"
$myModification1.Name = "add[@name='Access-Control-Allow-Origin'][@value='http://wtcsps99']"
$myModification1.Sequence = 0
$myModification1.Owner = "CrossSiteScripting"
#0 = for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$myModification1.Type = 0
$myModification1.Value = "<add name='Access-Control-Allow-Origin' value='http://wtcsps99' />"
$webapp.WebConfigModifications.Add($myModification1) $myModification1 = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$myModification1.Path = "configuration/system.webServer/httpProtocol/customHeaders"
$myModification1.Name = "add[@name='Access-Control-Request-Method'][@value='GET,POST,HEAD,OPTIONS']"
$myModification1.Sequence = 0
$myModification1.Owner = "CrossSiteScripting"
$myModification1.Type = 0
$myModification1.Value = "<add name='Access-Control-Request-Method' value='GET,POST,HEAD,OPTIONS' />"
$webapp.WebConfigModifications.Add($myModification1) $myModification1 = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$myModification1.Path = "configuration/system.webServer/httpProtocol/customHeaders"
$myModification1.Name = "add[@name='Access-Control-Request-Headers'][@value='Content-Type,Authorization']"
$myModification1.Sequence = 0
$myModification1.Owner = "CrossSiteScripting"
$myModification1.Type = 0
$myModification1.Value = "<add name='Access-Control-Request-Headers' value='Content-Type,Authorization' />"
$webapp.WebConfigModifications.Add($myModification1) $myModification1 = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$myModification1.Path = "configuration/system.webServer/httpProtocol/customHeaders"
$myModification1.Name = "add[@name='Access-Control-Allow-Credentials'][@value='true']"
$myModification1.Sequence = 0
$myModification1.Owner = "CrossSiteScripting"
$myModification1.Type = 0
$myModification1.Value = "<add name='Access-Control-Allow-Credentials' value='true' />"
$webapp.WebConfigModifications.Add($myModification1) $webapp.Update()
[Microsoft.SharePoint.Administration.SPWebService]::ContentService.ApplyWebConfigModifications() #Wait until web.config modifications finished by timer job
while( (Get-SPTimerJob | ? { $_.Name -eq "job-webconfig-modification"}) -ne $null ) {
Write-Host "." -NoNewline
Start-Sleep 1
}
另外也需要在javascript里面进行相关配置,具体如下:
var Module = window.Module || {};
Module.GetTasks = (function () {
var pub = {},
_userId, //userID of current user
_tasks = [], //List of our tasks
_options = {
listName: "Tasks List", //Name of list we want
container: "#TaskListContainer",
pulicUrl: "http://wtcsps98/sites/bpmmgmt"//id of html element we're rendering our list of tasks in
};
pub.init = function () {
var clientContext = new SP.ClientContext.get_current();
_userId = clientContext.get_web().get_currentUser();
clientContext.load(_userId); //clientContext.load(_userId);
clientContext.executeQueryAsync(GetDocDetail, _onQueryFailed); }; function ReturnTaskCount(results) {
var _tasks = results.d.results;
var count = 0;
var year = '2016';
$.each(_tasks, function (index, task) {
console.log(task.Status);
year = GetYearValue(task.Title);
if (parseInt(year) > 2014 && task.Status == 'Not Started') {
count++;
} });
return count;
}
function getCountFormDigest(webUrl, listName) {
var count = 0;
$.support.cors = true;
$.ajax({
url: webUrl + "/_api/contextinfo",
method: "POST",
headers: { "Accept": "application/json; odata=verbose" },
async: false,
xhrFields: { "withCredentials": true },
crossDomain: true,
success: function (data) {
count = getCountBySpecifiedList(data, listName, webUrl);
},
error: function (error) {
console.log("Error in getting List: " + error);
//$(_options.container).html("Error retrieving your " + listName + ".");
}
});
return count;
}
function GetYearValue(taskTitle) {
var year = '2016';
var sTitle = taskTitle;
var aTitle = sTitle.split('-');
var title = aTitle[2].replace(/^\s*/, '');
year = title.substring(3, 7);
console.log(year);
return year;
}
function getCountBySpecifiedList(data, listName, webUrl) {
var url = '', caml = '';
var count = 0;
var viewXml = "<View><Query><Where><Or><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID/></Value></Eq><Membership Type='CurrentUserGroups'><FieldRef Name='AssignedTo' /> </Membership></Or></Where></Query></View>";
//var viewXml = "<View><Query><Where><And><And><Or><Eq><FieldRefName='AssignedTo'/><ValueType='Integer'><UserID/></Value></Eq><MembershipType='CurrentUserGroups'><FieldRefName='AssignedTo'/></Membership></Or><Neq><FieldRefName='Status'/><ValueType='Choice'>Completed</Value></Neq></And><Gt><FieldRefName='Created'/><ValueType='DateTime'>2014-09-28T12:00:00Z</Value></Gt></And></Where></Query></View>"
$.support.cors = true;
$.ajax({
url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/getitems",
method: "POST",
async: true,
//xhrFields: { "withCredentials": true },
crossDomain: true,
beforeSend: function(request) {
request.setRequestHeader('Access-Control-Allow-Origin', 'http://wtcsps99');
request.withCredentials = true;
},
data: "{ 'query' : {'__metadata': { 'type': 'SP.CamlQuery' }, \"ViewXml\": \""
+ viewXml + "\" }}",
headers: {
"X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue,
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose"
},
success: function (results) {
count = ReturnTaskCount(results);
},
error: function (error) {
console.log("Error in getting List: " + listName);
//$(_options.container).html("Error retrieving your " + listName + ".");
}
});
return count;
}
function GetDocDetail() {
console.log(_userId.get_loginName());
var loginName = _userId.get_loginName();
if (loginName == 'i:0#.w|wtc\\setup.moss') {
$(_options.container).html('its over');
//return; }
var showHtml = '';
showHtml = '<div class="navitems_mc">';
showHtml += '<ul class="activeTit">';
$.support.cors = true;
$.ajax({
url: _options.pulicUrl + "/_api/web/lists/GetByTitle('Application Information')/items",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" },
async: false,
xhrFields: { withCredentials: true },
crossDomain: true,
beforeSend: Loading,
success: function (data) {
var appInfo = data.d.results;
if (appInfo) {
var count = 0;
var taskUrl = '';
var countTask = 0;
$.each(appInfo, function (index, item) {
taskUrl = item.SiteUrl + '/Lists/Tasks/MyItems.aspx';
countTask = getCountFormDigest(item.SiteUrl, item.TaskListName);
if (countTask > 0) {
showHtml += '<li class="navitems_mc_ul">';
showHtml += '<div class="classifydivv2" ><h2>' + item.CName + '</h2>';
showHtml += '<a href="' + taskUrl + '"><em>' + countTask + '</em></a>';
showHtml += '</div>';
showHtml += '</li>';
} });
}
},
error: function (xhr) {
console.log(xhr.status + ': ' + xhr.statusText);
}
});
showHtml += '</ul></div>';
$(_options.container).html(showHtml);
}
function Loading() {
var showHtml = '<b>数据加载中,请稍后...</b>';
$(_options.container).html(showHtml);
}
function _onQueryFailed(sender, args) {
alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
} return pub; }());
解决SHAREJPOINT 跨域问题的更多相关文章
- 解决cookie跨域访问
一.前言 随着项目模块越来越多,很多模块现在都是独立部署.模块之间的交流有时可能会通过cookie来完成.比如说门户和应用,分别部署在不同的机器或者web容器中,假如用户登陆之后会在浏览器客户端写入c ...
- 如何解决ajax跨域问题(转)
由 于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决 ajax的跨域问题.本篇将讲述一个小白从遇到跨域不 ...
- 用nginx的反向代理机制解决前端跨域问题
什么是跨域以及产生原因 跨域是指a页面想获取b页面资源,如果a.b页面的协议.域名.端口.子域名不同,或是a页面为ip地址,b页面为域名地址,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制 ...
- 用iframe设置代理解决ajax跨域请求问题
面对ajax跨域请求的问题,想用代理的方式来解决这个跨域问题.在服务器端创建一个静态的代理页面,在客户端用iframe调用这个代理 今天在项目中需要做远程数据加载并渲染页面,直到开发阶段才意识到aja ...
- CP="CAO PSA OUR" 用P3P header解决iframe跨域访问cookie
1.IE浏览器iframe跨域丢失Session问题 在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上 ...
- 看小白如何解决ajax跨域问题
由于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决ajax的跨域问题.本篇将讲述一个小白从遇到跨域不知道 ...
- 如何解决ajax跨域问题
如何解决ajax跨域问题(转) 由 于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决 ajax的跨域问题 ...
- 解决js跨域问题
如何解决js跨域问题 Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下 的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨 ...
- 说说JSON和JSONP,浅析JSONP解决AJAX跨域问题
说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...
随机推荐
- c#中将字符串转换成带2位小数的浮点数
今天遇到一个展示酒店价格的需求,觉得是要显示成“¥0.00”样式的,就做个小随笔,将字符串装换成带2位小数的浮点数 代码如下 "; string amount = string.Empty; ...
- Windows server 2008 被ntlmssp安装攻击 解决
进行NTLM策略控制,彻底阻止LM响应
- js中实现 复制到剪切板 功能
一:引包 <script type="text/javascript" src="jquery.js"></script> <sc ...
- spring自动类型转换========Converter和PropertyEditor
Spring有两种自动类型转换器,一种是Converter,一种是propertyEditor. 两者的区别:Converter是类型转换成类型,Editor:从string类型转换为其他类型. 从某 ...
- 链栈的基本操作(C语言)
栈的链式储存结构称为链栈.链栈的节点类型与链式线性表的节点类型 定义相同,不同的是它是仅在表头进行操作的单链表.链栈通常用不带头节 点的单链表来实现,栈顶指针就是链表的头指针 ,如图所示: 代码如下: ...
- mysql 在linux下的完整安装过程
1.下载RPM包 https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar 2.先使用命令删 ...
- C++STL stack
stack栈 先进后出 stack<int> s ; s.push();//元素入栈 //出栈 while(!s.empty()){ int tmp = s.top(); s.pop(); ...
- java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory的解决办法
Tomcat7早就出来正式版,但是一直都没有用过,尤其是针对于我还一直在用Myeclipse6.5的人来说,它在配置tomcat的时候没有tomcat7的选项,所以就报了错误信息. java.lang ...
- mathematica入门学习记录:
http://v.qq.com/vplus/4bc1736725fc7c3567d5bd9617482a49/foldervideos/m8k0000011aqj4k mathematica的数据 简 ...
- HDU-3608 最长回文
HDU-3608 最长回文 题面 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如ab ...