【问题标题】:Make AJAX request from MS CRM events从 MS CRM 事件发出 AJAX 请求
【发布时间】:2011-05-04 22:19:55
【问题描述】:

我需要从 MS CRM Dynamics 4.0 中表单的 onSave 事件向某些脚本发出 AJAX 请求。 我现在的代码是

var http_request;
// Prepare the xmlHttpObject and send the request.
try{
    http_request = new ActiveXObject("Msxm12.XMLHTTP");
}catch(e){
    try{
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){
        alert("Something went wrong..");
    }
}
var poststr = "foo=bar";
http_request.open("POST", "/folder/index.html", false);
http_request.setRequestHeader("Content-Type","text/xml; charset=utf-8"); 
http_request.send(escape(poststr));
// Capture the result.
var resultXml = http_request.responseText;
alert(resultXml);

警报现在向我提供 404 类型错误的内容。我确定该页面在那里,可以通过浏览器访问。 如果我改变
http_request.open("POST", "/folder/index.html", false);

http_request.open("POST", "localhost:5555/folder/index.html", false);
open() 失败,说“权限被拒绝”。

更新(2009 年 12 月 7 日);

我在 CRM 的 ISV 文件夹中创建了一个虚拟目录,并上传了一个 ASP.NET 应用程序。现在,如果我去 crm.url.nl:5555/ISV/Default.aspx 我得到;

'Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 不存在。 参数名称:Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

使用堆栈跟踪;

[ArgumentException: 'Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' doesn't exist.
Parameter name: Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]
   Microsoft.Crm.Authentication.BaseAuthenticationSettings.CreateType(String typeName, Type requiredBaseType) +265
   Microsoft.Crm.Authentication.BaseAuthenticationSettings.CreateProvider(String typeName, IDictionary`2 configuration) +28
   Microsoft.Crm.Authentication.AuthenticationPipelineSettings.LoadPipeline() +262
   Microsoft.Crm.Authentication.AuthenticationPipelineSettings.get_AuthenticationProvider() +16
   Microsoft.Crm.Authentication.AuthenticationEngine.Execute(Object sender, EventArgs e) +524
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

有人知道吗? .NET 应用程序只向 Response 写入一个单词,所以没有什么特别的......

【问题讨论】:

    标签: dynamics-crm dynamics-crm-4


    【解决方案1】:

    好的,我发现了这个想法。 如果您想从 CRM 4.0 对动态 .NET 应用程序执行 AJAX 调用,请执行以下操作。

    将 .NET 应用程序的程序集放在 CRM 文件夹中的 CRMWeb/bin 文件夹中。将您的 aspx 文件放在 ISV 文件夹中的文件夹中。我使用了 stunnware.com 文件夹,因为它在那里,但为了整洁,您可能需要创建另一个文件夹。 然后,在 onSave(或任何 on- 事件)中放置类似这样的内容;

    var xmlHttp = null;
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    
    var getstr = "foo=bar&foo2=bar2";
    var url = prependOrgName("/ISV/*YOURFOLDER*/Default.aspx?"+getstr);
    
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    

    当然,如果您希望 CRM 对 xml 请求的响应做任何事情,您可以使用 onreadystatechange。

    希望这可以帮助其他人。这可能看起来很简单,但我花了很长时间才弄清楚如何去做(尽管我知道如何使用 AJAX 和 .NET 之类的东西)。我认为 CRM 开发人员被 Microsoft 冷落是一种耻辱。他们真的应该花更多时间来记录 SDK 以及如何执行此类操作。

    【讨论】:

      【解决方案2】:

      只是想在这里尝试一下:

      文件的路径在 CRMWeb 目录中?您是否设置了虚拟目录或其他任何东西?

      /folder/index.html 是否在同一表单的 IFRAME 中工作?

      也许尝试一些您知道会起作用的方法,以确保:http://stackoverflow.comhttp://google.com

      另请注意,MS 建议将所有自定义项放在 /ISV/ 文件夹中。它不应该导致 404 错误,但我不确定他们是否会认为这是不受支持的。

      你真的在调用 html 扩展吗?我相信您需要为 aspx 和 asmx 扩​​展使用 prependOrgName(请参阅 SDK)。我不确定这是否会导致 404 或只是使用您的默认组织。

      【讨论】:

      • 感谢您的回复。我已将我的文件放在 /ISV/ 文件夹中,如果我将它包含在表单中的 IFRAME 中,它就可以工作。我得到的错误不是原始问题中发布的 404,而是 405(HTTP 动词不允许)。
      • 现在可以使用 /ISV/index.php,但如果我将其更改为 doSync.asp,我会得到与以前相同的 405 错误。 CRM 有没有可能只允许我做静态的事情?
      • 不,你可以做动态的东西。也许尝试 aspx (2.0+) 并针对它而不是 asp (1.1) 进行编译。 Crm 应用程序池将使用 2.0 框架,我不确定 2.0 如何处理 1.1。这就是我要开始的地方,特别是如果您知道您对 php 示例没有任何问题。我认为 php 脚本从未得到 405?
      • web.config 中有用于启用 POST 和 GET 动词的节点。我相信它们在默认的 web.config 中,但可能没有为 ISV 文件夹启用它们,或者它们已被删除?
      猜你喜欢
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 2016-11-25
      • 2017-09-27
      • 1970-01-01
      • 2019-02-25
      相关资源
      最近更新 更多