365通过JavaScript异步调用

365通过JavaScript异步调用

本文介绍了Microsoft Dynamics CRM 365通过JavaScript异步调用操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种无需使用第三方脚本即可通过javascript调用动作的方法?

Is there a way to call an action via javascript without the use of third party scripts?

我发现了这个 https://github.com/PaulNieuwelaar/processjs

但是,我不能使用第三方库.

However, I cannot use third party libraries.

更新:

这是一些示例代码,演示了通过JavaScript异步调用动作的过程.要记住的重要一点是将请求的open方法的最后一个参数设置为 true .

Here is some sample code that demonstrates an asynchronous call to an action via JavaScript. A important point to remember is to make the last parameter of the open method of the request to true.

req.open(consts.method.post, oDataEndPoint, true);

//插件

   public class RunAsync : CodeActivity
    {
        [Input("input")]
        public InArgument<string> Input { get; set; }

        [Output("output")]
        public OutArgument<string> Output { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                Thread.Sleep(20000);
                Output.Set(executionContext, $"Result:{Input.Get(executionContext)}");
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }

//javascript

// javascript

function callAction(actionName, actionParams, callback) {

    var result = null;
    var oDataEndPoint = encodeURI(window.Xrm.Page.context.getClientUrl() + consts.queryStandard + actionName);

    var req = new XMLHttpRequest();
    req.open(consts.method.post, oDataEndPoint, true);
    req.setRequestHeader(consts.odataHeader.accept, consts.odataHeader.applicationJson);
    req.setRequestHeader(consts.odataHeader.contentType, consts.odataHeader.applicationJson + ";" + consts.odataHeader.charset_utf8);
    req.setRequestHeader(consts.odataHeader.odataMaxVersion, consts.odataHeader.version);
    req.setRequestHeader(consts.odataHeader.odataVersion, consts.odataHeader.version);
    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            req.onreadystatechange = null;
            if (req.status === 200) {
                if (callback) {
                    result = JSON.parse(this.response);
                    callback(result);
                }
            } else {
                console.log(JSON.parse(this.response).error);
            }
        }
    };
    req.send(JSON.stringify(actionParams));
}

function onLoad() {

    console.log('call action...');

    var actionParams = {
        Input: 'test1234'
    };

    callAction('TestAsyncAction',actionParams, function(data){
        console.log('action callback triggered...');
        console.log(JSON.stringify(data));
    });

    console.log('action called...');
}

//动作

推荐答案

您可以使用 webapi执行自定义操作.它包装在XMLHttpRequest&中.可以称为异步.

You can use webapi to execute custom Action. This is wrapped in XMLHttpRequest & can be called asynchronous.

/api/data/v8.2/Action_Name

对于异步运行:

req.open(....., true);

使用肥皂呼叫(不是推荐的).

The same using soap call (not recommended).

Processjs使用将要弃用的Organization.svc/web.

这篇关于Microsoft Dynamics CRM 365通过JavaScript异步调用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:41