gpt4 book ai didi

cordova - 使用phonegap更新SharePoint列表项

转载 作者:行者123 更新时间:2023-12-03 04:30:32 24 4
gpt4 key购买 nike

我可以使用 azure ad 和 azure-activedirectory-library-for-cordova 对共享点进行身份验证。我能够从共享点获取列表,但无法更新或创建列表项。这是我用于身份验证和获取列表的代码。我需要的是创建或更新列表项。我指的是This example

var authority = "https://login.windows.net/common",
redirectUri = "http://my re direct url",
resourceUri = "https://my resource url",
clientId = "5a9hh56u-2485-7523-0122-j5k62463ab05",

var app = {
// Invoked when Cordova is fully loaded.
onDeviceReady: function() {
document.getElementById('search').addEventListener('click', app.search);
},
// initialize authentication operations.
search: function () {
app.authenticate(function (authresult) {

app.requestData(authresult);
});
},
// Shows user authentication dialog if required.
authenticate: function (authCompletedCallback) {

app.context = new Microsoft.ADAL.AuthenticationContext(authority);
app.context.tokenCache.readItems().then(function (items) {
if (items.length > 0) {
authority = items[0].authority;
app.context = new Microsoft.ADAL.AuthenticationContext(authority);
alert(app.context.webAbsoluteUrl);
}
// Attempt to authorize user silently
app.context.acquireTokenSilentAsync(resourceUri, clientId)
.then(authCompletedCallback, function () {
// We require user cridentials so triggers authentication dialog
app.context.acquireTokenAsync(resourceUri, clientId, redirectUri)
.then(authCompletedCallback, function (err) {
app.error("Failed to authenticate: " + err);
});
});
});

},
// Makes Api call to receive user list.
requestData: function (authResult) {
alert("Token : "+authResult.accessToken);
var req = new XMLHttpRequest();
var url = "https://mytenant.sharepoint.com/_api/web/lists/getbytitle('WebServiceTest')/items";
req.open("GET", url, true);
req.setRequestHeader('Authorization', 'Bearer ' + authResult.accessToken);

req.onload = function(e) {
if (e.target.status >= 200 && e.target.status < 300) {
alert("target"+e.target.request);
var xml = $.parseXML(e.target.response),
$xml = $( xml ),
$test = $xml.find('Title');
alert($test.text());
$("#userlist").text($test.text());
return;
}
app.error('Data request failed: ' + e.target.response);
};
req.onerror = function(e) {
app.error('Data request failed: ' + e.error);
}

req.send();
},

error: function(err) {
var userlist = document.getElementById('userlist');
userlist.innerHTML = "";
var errorItem = document.createElement('li');
errorItem.classList.add('topcoat-list__item');
errorItem.classList.add('error-item');
errorItem.innerText = err;
userlist.appendChild(errorItem);
}
};

document.addEventListener('deviceready', app.onDeviceReady, false);

最佳答案

您可能需要执行 POST 来更新/创建项目,而不是获取。像这样:

$.ajax({
url: url + "/_api/web/lists/getbytitle('WebServiceTest')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
success(data); // Returns the newly created list item information
},
error: function (data) {
failure(data);
}
});

关于cordova - 使用phonegap更新SharePoint列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383075/

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