如何在 Mozilla 附加 SDK 扩展中将变量传递给请求 API?

How to pass Variable to Request API in Mozilla Add-on SDK extension?

我正在尝试将变量值传递给 Mozilla Add-on SDK 扩展的 Request API

在下面的代码中,fetch Request 有效。但是,当我尝试在 onCompletion 处理程序中为 fetch 请求另一个 Request (assign) 时,它说:

RequirementError: The option "url" is invalid.

如何使用 onCompletion 处理程序中的 URL 用于 fetch Request 用于 assign 中的 URL Request?

var fetch = Request({
    url: 'http://itildemo.servicedeskplus.com/sdpapi/request?INPUT_DATA={%22operation%22:{%22details%22:{%22status%22:%22open%22,%22from%22:0,%22limit%22:500,%22filterby%22:%22Unassigned_System%22}}}&OPERATION_NAME=GET_REQUESTS&TECHNICIAN_KEY=07109ADB-E642-4F46-917C-CA60F89CE6DC&format=json',
    overrideMimeType: "text/plain; charset=latin1",
    onComplete: function (response) {
        workorder_id=JSON.parse(response.text).operation.details;
        if(workorder_id!=null){
            assignurl=urls.URL('http://itildemo.servicedeskplus.com/sdpapi/request/'+workorder_id[0].WORKORDERID+'?OPERATION_NAME=EDIT_REQUEST&TECHNICIAN_KEY=07109ADB-E642-4F46-917C-CA60F89CE6DC&INPUT_DATA= <Details><parameter><name>technician</name><value>'+technician_id[0].NAME+'</value></parameter></Details>');
            console.log(assignurl);
            assign.get();}
        else{fetch.get();}
    }
});

var assign = Request({
    url: assignurl,
    overrideMimeType: "text/plain; charset=latin1",
    onComplete: function (response) {
          var o=technician_id[0];
          for (var k = 0; k < technician_id.length; k++) {
              if (technician_id.length - 1 == k) {
                  technician_id[k] = o;
              } else {
                  technician_id[k] = technician_id[k + 1];
              };
          };
          fetch.get();
    }
});

您对异步代码的执行顺序感到困惑。

如上所写,

var assign = Request({
    url: assignurl,

在回调函数执行提供 assignurl:

的行之前执行
assignurl=urls.URL('http:// ...

通过调用 Request() constructor,(var assign = Request({),您已将 url 分配给当时存在的 assignurl。在该点之后更改变量 assignurlassign Request 没有影响。因此,您要么需要更改 assign.url,要么在 URL 可用后创建请求。

注意: 在调用 Request 构造函数时,url 参数必须存在且有效。因此,我假设您的问题在陈述错误时是不正确的:

RequirementError: The option "url" is invalid.

仅在 fetchonCompletion 处理程序中发生。当您使用无效的 assignurl.

执行 var assign = Request({url:asignurl 时应该会发生这种情况

您可以通过多种方式更改代码以在 Request 中使用您现在拥有的 URL(通过 fetch onComplete 回调)您正在为 assign 创建。使用哪种方法取决于您是否要在代码中的多个位置使用与 assign 非常相似的请求。

一个可能的更改是将 assign Request 的创建移动到 fetchonCompletion 处理程序中:

var Request = require("sdk/request").Request;
var assignurl;          //Unknown from Question
var technician_id=[];   //Unknown from Question
var workorder_id;       //Unknown from Question

var assign;
var fetch = Request({
    url: 'http://itildemo.servicedeskplus.com/sdpapi/request?INPUT_DATA={%22operation%22:{%22details%22:{%22status%22:%22open%22,%22from%22:0,%22limit%22:500,%22filterby%22:%22Unassigned_System%22}}}&OPERATION_NAME=GET_REQUESTS&TECHNICIAN_KEY=07109ADB-E642-4F46-917C-CA60F89CE6DC&format=json',
    overrideMimeType: "text/plain; charset=latin1",
    onComplete: function (response) {
        workorder_id=JSON.parse(response.text).operation.details;
        if(workorder_id!=null){
            assignurl=urls.URL('http://itildemo.servicedeskplus.com/sdpapi/request/'+workorder_id[0].WORKORDERID+'?OPERATION_NAME=EDIT_REQUEST&TECHNICIAN_KEY=07109ADB-E642-4F46-917C-CA60F89CE6DC&INPUT_DATA= <Details><parameter><name>technician</name><value>'+technician_id[0].NAME+'</value></parameter></Details>');
            assign = Request({
                url: assignurl,
                overrideMimeType: "text/plain; charset=latin1",
                onComplete: function (response) {
                    var o=technician_id[0];
                    for (var k = 0; k < technician_id.length; k++) {
                        if (technician_id.length - 1 == k) {
                            technician_id[k] = o;
                        } else {
                            technician_id[k] = technician_id[k + 1];
                        };
                    };
                    fetch.get(); //This will throw an error (see below).  Even if it
                                 //  worked it is recursive, which is probably not
                                 //  what you desire.
                }
            });
            console.log(assign.url);
            assign.get();}
        else{
            fetch.get(); //This should always throw an error as you are attempting to
                         //  re-use the Request. In other words, the only way to get
                         //  here is if you have already done fetch.get() once
                         //  and reusing the Request throws an error. 
        }
    }
});

如果您可能会发出多个 assign 请求,您可以构建代码,以便创建 assign Request 封装在一个 function 中,您可以调用多个多次创建多个非常相似的请求,如果您需要在代码的其他地方这样做:

var Request = require("sdk/request").Request;
var technician_id=[];   //Unknown from Question
var workorder_id;       //Unknown from Question

var assign;
var fetch = Request({
    url: 'http://itildemo.servicedeskplus.com/sdpapi/request?INPUT_DATA={%22operation%22:{%22details%22:{%22status%22:%22open%22,%22from%22:0,%22limit%22:500,%22filterby%22:%22Unassigned_System%22}}}&OPERATION_NAME=GET_REQUESTS&TECHNICIAN_KEY=07109ADB-E642-4F46-917C-CA60F89CE6DC&format=json',
    overrideMimeType: "text/plain; charset=latin1",
    onComplete: function (response) {
        workorder_id=JSON.parse(response.text).operation.details;
        if(workorder_id!=null){
            assign = setupAssignRequest(urls.URL('http://itildemo.servicedeskplus.com/sdpapi/request/'+workorder_id[0].WORKORDERID+'?OPERATION_NAME=EDIT_REQUEST&TECHNICIAN_KEY=07109ADB-E642-4F46-917C-CA60F89CE6DC&INPUT_DATA= <Details><parameter><name>technician</name><value>'+technician_id[0].NAME+'</value></parameter></Details>'));
            console.log(assign.url);
            assign.get();}
        else{
            fetch.get(); //As stated above, this should always throw an error.
        }
    }
});

function setupAssignRequest(assignurl) {
    return Request({
        url: assignurl,
        overrideMimeType: "text/plain; charset=latin1",
        onComplete: function (response) {
            var o=technician_id[0];
            for (var k = 0; k < technician_id.length; k++) {
                if (technician_id.length - 1 == k) {
                    technician_id[k] = o;
                } else {
                    technician_id[k] = technician_id[k + 1];
                };
            };
            fetch.get(); //If the only code you have is what is shown, this will
                         // throw an error. It is possible that "fetch" has been 
                         // re-initialized with a new call to Request elsewhere
                         // in your code.
        }
    });
}

关于 fetchonComplete 处理程序中的 fetch.get();:尝试这样做应该总是导致抛出错误。获得该代码的唯一方法是已经调用 fetch.get();each Request can only be used once:

Each Request object is designed to be used once. Attempts to reuse them will throw an error.