在VSTS扩展名中加载JSON文件 [英] Loading a JSON file in a VSTS extension
问题描述
我正在尝试编写一个VSTS扩展,该扩展需要加载(和解析)一个JSON文件,但是我很难找到正确的方法来实现它.
I'm trying to write a VSTS extension that needs to load (and parse) a JSON file but I'm having a hard time finding the right way to do it.
所以我有类似的东西:
VSS.init({
explicitNotifyLoaded: true,
usePlatformScripts: true
});
var witClient;
var rules;
VSS.ready(function () {
require(["fs"], function (fs) {
rules = JSON.parse(fs.readFileSync("urlMatches.json"));
})
VSS.require(["VSS/Service", "TFS/WorkItemTracking/RestClient"], function (VSS_Service, TFS_Wit_WebApi) {
// Get the REST client
witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
});
// Register a listener for the work item page contribution.
VSS.register(VSS.getContribution().id, function () {
return {
// Called after the work item has been saved
onSaved: function (args) {
witClient.getWorkItem(args.id).then(
function (workItem) {
// do some stuff involving the loaded JSON file...
});
}
}
});
VSS.notifyLoadSucceeded();
});
我已经尝试过很多方法,但是没有任何运气.我什至尝试使用jQuery同步加载文件,但我的 rules
最终未定义.
I've tried a bunch of variations on this without any luck. I've even tried using jQuery to load my file synchronously and yet my rules
ends up undefined.
所以我有文件 urlmatches.json
,我需要加载它并使用它来填充变量 rules
,然后再进入 onSaved 代码>处理程序.
So I have the file urlmatches.json
and I need to load it and use it to populate the variable rules
before I get to the onSaved
handler.
推荐答案
您可以通过HTTP请求检索内容,例如:
You can retrieve content through HTTP request, for example:
onSaved: function (args) {
console.log("onSaved -" + JSON.stringify(args));
var request = new XMLHttpRequest();
request.open('GET', 'TestData.txt', true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
var type = request.getResponseHeader('Content-Type');
console.log(request.responseText);
}
}
这篇关于在VSTS扩展名中加载JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!