有没有办法在不使用第三方脚本的情况下通过 javascript 调用操作?
我发现了这个https://github.com/PaulNieuwelaar/processjs
但是,我不能使用第三方库。
更新:
下面是一些示例代码,演示了通过 JavaScript 对操作的异步调用。要记住的重要一点是使请求的 open 方法的最后一个参数为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
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...');
}
// 行动