Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top