我是超级账本作曲家的初学者。我想从服务器(如AJAX)中检索数据,这些数据在 hyperledger composer 的 javascript 文件中使用。
我怎样才能实现它?
下面是我在 hyperledger composer 的脚本文件中使用的w3school 的示例。
/** * 示例事务处理器函数。 * @param {org.acme.sample.SampleTransaction} tx 示例交易实例。 * @transaction */ function sampleTransaction(tx) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); }
您可以在 Composer 事务函数中使用调出。但请记住,对于智能合约交易,所有执行交易逻辑的节点都必须返回一个确定性的结果——否则你的交易将无法被认可(你可能知道,只是说)
在此处查看更多详细信息和示例 -> https://hyperledger.github.io/composer/latest/integrating/call-out
function handlePost(postTransaction) {
var url = 'https://composer-node-red.mybluemix.net/compute';
// call-out
return post( url, postTransaction)
.then(function (result) {
// alert(JSON.stringify(result));
postTransaction.asset.value = 'Count is ' + result.body.sum;
// now update an Asset Registry (Composer)
return getAssetRegistry('org.example.sample.SampleAsset')
.then(function (assetRegistry) {
return assetRegistry.update(postTransaction.asset);
});
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句