0

I'm new to working with sharepoint. When i tried to call share point API url from ajax call in JavaScript. But, it's continuously failing and throws the error. I can say, that is CORS error. So, i've made changes in web.config like below.

<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type,Accept,X- FORMS_BASED_AUTH_ACCEPTED,crossDomain,credentials " />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />

and ajax call as below

 $.ajax({
            url: url,
            headers: {
                Accept: "application/json;odata=verbose"
            },
            xhrFields: { withCredentials: true },
            async: false,
            success: function (data) {
                var items = data.d;
                console.log("Login Name: " + items.LoginName);
                console.log("Email: " + items.Email);
                console.log("ID: " + items.Id);
                console.log("Title: " + items.Title);
            },
            error: function (jqxr, errorCode, errorThrown) {
                console.log(jqxr.responseText);
            }
        });

Please help me regarding this. I'm trying since 2 days.

Update:

This error I'm getting.

Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

  • Are you calling the SharePoint API from application outside SharePoint? – Sohail Shaikh May 30 at 5:29
  • Yes, I mean the html page is not in the same site(IIS site) where SharePoint site is actually hosted. – Chanikya May 30 at 5:39
  • Did you restarted the IIS site (the one you changed in web.config) ? – Sohail Shaikh May 30 at 5:44
  • Yes, I've updated the question. I've added the error which i'm getting. I can understand the error. But, i don't know what to do to resolve the error. – Chanikya May 30 at 5:45
0

Use below Access control allow origin settings in web.config:

<add name="Access-Control-Allow-Origin" value="http://localhost:62747" />

As the error states, you shouldn't use the wildcard. Use the application url.

Edit: (Fix for Data will not come via ajax call but you can see under response in Network tab)

Disable the Request Management service on the SharePoint server. Following are the steps to do it.

Go to

  1. Central Admin Application Management
  2. Manage services on server (under Service Applications)
  3. and Stop the Request Management service.

Reference

  • 1
    Sure, i'll try and let you know. Please i'm requesting you to be available. Thanks. – Chanikya May 30 at 5:49
  • @Chanikya Please upvote and mark the answer as accepted in case it helped you. – Sohail Shaikh May 30 at 6:17
  • Can i give my HTML file path instead of 'localhost:62747'. Like 'E:/Folder/test.html' this is my path. – Chanikya May 30 at 10:54
  • @Chanikya You can try that or else you can open the html in visual studio that will generate a http local host url. – Sohail Shaikh May 30 at 11:24
  • 1
    Thanks @Sohail Shaikh – Chanikya Jun 3 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.