使用 jquery 的 UserProfile REST API 在 google chrome 中不起作用

UserProfile REST API using jquery is not working in google chrome

用户配置文件 REST API 调用在 google chrome

中不起作用

以下代码:-

$.ajax({
  url: "http://<site url>/_api/sp.userprofiles.peoplemanager
    /getpropertiesfor(@v)?@v='i%3A0%23.f%7Cmembership%7Cuser%40domain.onmicrosoft.com'",
  type: "GET",
  headers: { "accept": "application/json;odata=verbose" },
  success: successHandler,
  error: errorHandler
});

尝试将 cache: false 添加到 ajax 选项。他们是 url.

中的一个额外 '

请试试这个。让我知道是否有帮助

$.ajax({
  url: "http://<site url>/_api/sp.userprofiles.peoplemanager
    /getpropertiesfor(@v)?@v='i%3A0%23.f%7Cmembership%7Cuser%40domain.onmicrosoft.com",
  type: "GET",
  cache: false,
  dataType: "json",
  headers: { "accept": "application/json;odata=verbose" },
  success: successHandler,
  error: errorHandler
});

已更新

请参考此 link 因为它会告诉您为什么会得到 403 Forbidden status code

摘要:

The 403 Forbidden response. It’s permanent, it’s tied to my application logic, and it’s a more concrete response than a 401.

Receiving a 403 response is the server telling you, “I’m sorry. I know who you are–I believe who you say you are–but you just don’t have permission to access this resource. Maybe if you ask the system administrator nicely, you’ll get permission. But please don’t bother me again until your predicament changes.”

你的情况可能是

您无权对给定资源执行请求的操作。


交叉原点AJAX

    var accesstoken = localStorage.getItem('myApiSession');

    var authHeaders = {};
    if (accesstoken) {
        authHeaders.Authorization = 'Bearer ' + accesstoken;
    }

    $.ajax({
        url: 'http://localhost:13838' + link,
        type: "POST",
        cache: false,
        headers: authHeaders,
        success: function (data) {
            //console.log(data);
        },
        error: function (xhr) {
            console.log(xhr);
        }
    });

在 webApi 中

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class SearchController : ApiController
{
    public object Get(string str)
    {

        WebServiceBLL WebBLL = new WebServiceBLL(new NaqshaMaker2Entities());
        var stre = JsonConvert.SerializeObject(WebBLL.Search(str));

        return Json(new { result = stre }); 

    }

}

这是双方成功跨源请求的正确方式