0

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

1 Answers1

0

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

Community
  • 1
  • 1
Abdul Moiz Khan
  • 693
  • 5
  • 13
  • I am getting error of forbidden access(status code 403) in chrome but working in IE.cache: false, didn't work – Abhijeet Kumar Jul 31 '16 at 07:41
  • If you are having an `forbidden access` error. It means your `headers` are not correct. Are you using an api ? – Abdul Moiz Khan Jul 31 '16 at 09:45
  • what should be content of header. – Abhijeet Kumar Jul 31 '16 at 09:55
  • I cannot see your console log error. I think you missed them. Please type again. Thank you – Abdul Moiz Khan Jul 31 '16 at 09:58
  • error after passing authentication :----XMLHttpRequest cannot load https:///_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:14361' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute. – Abhijeet Kumar Jul 31 '16 at 09:59
  • you said after adding header your forbidden access was gone and you where recieving some other error. Can you tell me please what where those ? – Abdul Moiz Khan Jul 31 '16 at 10:00
  • i added xhrFields: { withCredentials: true }, for authentication – Abhijeet Kumar Jul 31 '16 at 10:02
  • You webapi is refusing the request as i told. In order to resolve it you must either add cross origion access to your webapi – Abdul Moiz Khan Jul 31 '16 at 10:02
  • new error was XMLHttpRequest cannot load https:///_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'localhost:14361'; is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute – Abhijeet Kumar Jul 31 '16 at 10:03
  • any helpful link to implement cross origion access to webapi – Abhijeet Kumar Jul 31 '16 at 10:04
  • withCredentials: false was again causing forbidden error – Abhijeet Kumar Jul 31 '16 at 10:05
  • In above example I have shown how can you implement a cross origin request. It is from a working project. Now one thing to not is `accesstoken` you need it in order to make it valid call. Else it will fail. Moreover you have to add `dataType: "json"` – Abdul Moiz Khan Jul 31 '16 at 10:15
  • I will give you link to implement cross origin request – Abdul Moiz Khan Jul 31 '16 at 10:16
  • http://stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery – Abdul Moiz Khan Jul 31 '16 at 10:22
  • WebApi should be added in my code? by using above 2 code,hopefully I would find my solution – Abhijeet Kumar Jul 31 '16 at 10:30