Sharepoint 2013:通过REST API的确保用户 [英] Sharepoint 2013: EnsureUser via REST API
问题描述
我正在尝试通过REST API自动确保某些用户. 我的REST通话:
I'm trying to ensure some users automatically via REST API. My REST call:
$.ajax({
url: "blablabla/_api/web/ensureuser",
type: "POST",
data: "{ 'logonName': 'i%3A0%23.w%7Cdomain%09logonName' }",
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"accept": "application/json;odata=verbose"
},
success: function () {
console.log("done!");
},
error: function (err) {
console.log(JSON.stringify(err));
}
});
现在发送此呼叫时,出现以下错误;
Now when sending this call I get the following error;
错误请求:Microsoft.Data.OData.ODataContentTypeException找不到与响应的内容类型匹配的受支持的MIME类型.所有受支持的类型'application/json; odata = verbose'与该响应类型都不匹配.内容类型'application/x-www-form-urlencoded; charset = UTF-8'"
"Bad Request: Microsoft.Data.OData.ODataContentTypeException A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/json;odata=verbose' matches the content type 'application/x-www-form-urlencoded; charset=UTF-8'"
The call is built like in the msdn reference specified.
推荐答案
由于是JSON请求,因此需要明确指定ContentType
,因此会发生此错误:
This error occurs since ContentType
needs to be specified explicitly since it's a JSON request:
contentType
(默认值:'application/x-www-form-urlencoded; charset = UTF-8')
contentType
(default: 'application/x-www-form-urlencoded; charset=UTF-8')
将数据发送到服务器时,请使用此内容类型.默认是 "application/x-www-form-urlencoded; charset = UTF-8"
When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8"
示例
function ensureUser(webUrl,loginName)
{
var payload = { 'logonName': loginName };
return $.ajax({
url: webUrl + "/_api/web/ensureuser",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(payload),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"accept": "application/json;odata=verbose"
}
});
}
var loginName = 'i:0#.f|membership|jdoe@contoso.onmicrosoft.com'
ensureUser(_spPageContextInfo.webAbsoluteUrl,loginName)
.done(function(data)
{
console.log('User has been added');
})
.fail(function(error){
console.log('An error occured while adding user');
});
这篇关于Sharepoint 2013:通过REST API的确保用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!