Search code examples
jquerygoogle-chromeoffice365-restapi

UserProfile REST API using jquery is not working in google chrome


User profiles REST API call is not working in google chrome

following code:-

$.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
});

Solution

  • try adding cache: false to ajax options. and their is one extra ' in your url.

    Try this please. let me know if it helps

    $.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
    });
    

    UPDATED

    Please refer to this link as it will give an idea for why you are getting 403 Forbidden status code

    For summary:

    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.”

    In your case it might be

    You are not authorized to perform the requested operation on the given resource.


    Cross Origion 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);
            }
        });
    

    In 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 }); 
    
        }
    
    }
    

    This is a correct way on both sides to make a successfull cross origin request