Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Calling REST API from javascript for Dynamics 365 online giving error

(0) ShareShare
ReportReport
Posted on by 505

I'm calling REST API from javascript. The API call fails and gives the following two errors

SEC7123: Request header loginrequestcorrelationid was not present in the Access-Control-Allow-Headers list.
SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

Below is my code:

$.ajax({
url: "mycrm.api.crm.dynamics.com/.../products(FCF2FCA4-8337-E811-A952-000D3A3026D6)$select=name,description,price",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
crossDomain: true,
xhrFields: {
cors: true
},
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

},
success: function (data, textStatus, xhr) {
alert(data.name)
},

error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
})

How do I fix this issue?

  • Verified answer
    Priyank Bhavsar Profile Picture
    Priyank Bhavsar 505 on at
    RE: Calling REST API from javascript for Dynamics 365 online giving error

    This issue was caused by hardcoded Web API URL. I changed it to XRM.Page.context.getClientUrl () + /api/data/v9.0.

    Now I'm not getting access denied error anymore.

  • Suggested answer
    RE: Calling REST API from javascript for Dynamics 365 online giving error

    Could you try using XRM.WebApi. Its easier and cleaner code.

    Please have a look at below samples.

    dreamingincrm.com/.../basic-crud-using-xrm-webapi

  • Suggested answer
    G Kawinski Profile Picture
    G Kawinski 380 on at
    RE: Calling REST API from javascript for Dynamics 365 online giving error
    Have you logged in and authenticated with your Office 365 tenant / domain? The client connecting to CRM requires that authentication token in the header. I also notice you're trying to do a cross-site call, so it looks like that might be the case. If you're using a browser, open multiple windows. In the first window, establish a connection to CRM. Now open a second window to test your code. From a design POV, you may want to create a server-side proxy web-service to query CRM instead of doing it directly from the client. Negotiating oauth security tokens in JavaScript would be a nightmare.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans