简体   繁体   English

来自外部站点的Dynamics CRM Web API(JavaScript)

[英]Dynamics CRM Web API from external site (javascript)

I'm currently developing a javascript app and I'm trying to access the Dynamics CRM Web API to fetch some information from the CRM. 我目前正在开发一个javascript应用程序,并且试图访问Dynamics CRM Web API以从CRM中获取一些信息。

My app hosted inside an Azure App Service (and testing in localhost), and it's accessible only authenticated users (by microsoft), so when users try to load the app, the azurewebsites redirect them to the microsoft's common OAUTH login page ( https://login.microsoftonline.com/common/oauth2/authorize?... ). 我的应用程序托管在Azure应用程序服务中(并在localhost中进行测试),并且只能由经过身份验证的用户访问(由Microsoft),因此,当用户尝试加载应用程序时,azurewebsites将其重定向到Microsoft的通用OAUTH登录页面( https://login.microsoftonline.com/common/oauth2/authorize?... )。

After a successful login, users redirect back to my javascript app, and then when the document is ready, I would like to call the dynamics CRM web api to fetch some entity (via jQuery's ajax request), but I get the following error message in the JS console: 成功登录后,用户重定向回我的javascript应用程序,然后在文档准备好后,我想调用dynamics CRM Web api来获取某些实体(通过jQuery的ajax请求),但是我收到以下错误消息: JS控制台:

XMLHttpRequest cannot load https://MYTENANTID.crm4.dynamics.com/api/data/v8.1/contacts. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://MYAPP.local' is therefore not allowed access. The response had HTTP status code 401.

I know this is bacause of the same origin policy, the question is: is it possible access the API from client side at all, or I need to do it in a server side? 我知道这是源于同一原始策略的原因,问题是:是否可以从客户端访问API,还是需要在服务器端进行访问?

According to your error message, it seems to be a common CORS issue when we calling a request from the client side application to our backend server. 根据您的错误消息,当我们从客户端应用程序向后端服务器调用请求时,这似乎是一个常见的CORS问题。

Usually, we will add response headers in backend server to support the CORS. 通常,我们将在后端服务器中添加响应头以支持CORS。 And you can refer to https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api for more info about how to enable Cross-Origin Requests in ASP.NET Web API. 您可以参考https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api了解有关如何在ASP中启用跨域请求的更多信息。 NET Web API。

And CRM 2016 has support CORS setting, please refer to https://msdn.microsoft.com/en-us/library/gg309589%28v=crm.8%29.aspx?f=255&MSPPError=-2147217396#bkmk_corsSupport for more info. 而且CRM 2016支持CORS设置,请参考https://msdn.microsoft.com/zh-cn/library/gg309589%28v=crm.8%29.aspx?f=255&MSPPError=-2147217396#bkmk_corsSupport了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM