gpt4 book ai didi

javascript - 如何在 ajax 调用中访问 Sharepoint REST api

转载 作者:行者123 更新时间:2023-11-30 06:15:39 28 4
gpt4 key购买 nike

我是 Sharepoint 的新手。当我尝试从 JavaScript 中的 ajax 调用调用共享点 API url 时。但是,它不断失败并抛出错误。我可以说,这是 CORS 错误。因此,我在 web.config 中进行了如下更改。

<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type,Accept,X- FORMS_BASED_AUTH_ACCEPTED,crossDomain,credentials " />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />

和ajax调用如下

 $.ajax({
url: url,
headers: {
Accept: "application/json;odata=verbose"
},
xhrFields: { withCredentials: true },
async: false,
success: function (data) {
var items = data.d;
console.log("Login Name: " + items.LoginName);
console.log("Email: " + items.Email);
console.log("ID: " + items.Id);
console.log("Title: " + items.Title);
},
error: function (jqxr, errorCode, errorThrown) {
console.log(jqxr.responseText);
}
});

请帮我解决这个问题。我从 2 天开始就在尝试。

Update:

我收到这个错误。

对预检请求的响应未通过访问控制检查:当请求的凭据模式为“时,响应中“Access-Control-Allow-Origin” header 的值不得为通配符“*”包括'。

最佳答案

使用下面的 PowerShell 脚本添加 web.config 修改:

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0

$localFarm = Get-SPFarm

$webapp = Get-SPWebApplication "http://sp:12001"

# 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://localhost:31699']"
$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://localhost:31699' />"
$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
}

然后像下面这样在 jQuery Ajax 中使用 REST API。

$.support.cors = true;
$.ajax({
url: url,
type: "GET",
crossDomain: true,
dataType: "json",
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
async: false,
success: function (data) {
var items = data.d;
console.log("Login Name: " + items.LoginName);
console.log("Email: " + items.Email);
console.log("ID: " + items.Id);
console.log("Title: " + items.Title);
},
error: function (jqxr, errorCode, errorThrown) {
console.log(jqxr.responseText);
}
});

引用:Cross Site Scripting with SharePoint 2013 REST calls

关于javascript - 如何在 ajax 调用中访问 Sharepoint REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372245/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com