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'"
调用的构建方式类似于在 msdn 参考中 指定.
The call is built like in the msdn reference specified.
推荐答案
出现这个错误是因为 ContentType
需要明确指定,因为它是一个 JSON 请求:
This error occurs since ContentType
needs to be specified explicitly since it's a JSON request:
contentType
(默认:'application/x-www-form-urlencoded;字符集=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屋!