如何在Mozilla Add-on SDK扩展中将Variable传递给Request API?

时间:2016-09-27 19:35:47

标签: javascript firefox-addon mozilla

我正在尝试将变量值传递给Mozilla附加SDK扩展的Request API

在下面的代码中,fetch Request正在运作。但是,当我尝试在Request的{​​{1}}处理程序中请求另一个assignonCompletion)时,它会说:

fetch

如何使用我RequirementError: The option "url" is invalid. 处理程序中的onCompletion fetch处理程序中的网址Request assign中的网址?

Request

1 个答案:

答案 0 :(得分:1)

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

如上所述,

var assign = Request({
    url: assignurl,

在回调函数执行提供assignurl

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

通过调用Request() constructor,(var assign = Request({),您已将当前存在的url指定为assignurl。在该点之后更改变量assignurl不会影响assign Request。因此,您需要更改assign.url,或者在URL可用后创建请求。

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

RequirementError: The option "url" is invalid.

仅发生在onCompletion的{​​{1}}处理程序中。当您使用无效fetch执行var assign = Request({url:asignurl时,应该会发生这种情况。

您可以通过多种方式更改代码,以使用您为Request创建的each Request can only be used once中现有的网址(来自assignurl fetch回调) 1}}。您使用哪种方法取决于您是否要在代码中的多个位置使用与onComplete非常相似的请求。

一种可能的变化是将assign assign的创建移动到assign的{​​{1}}处理程序中:

Request

如果您发出多个onCompletion请求,则可以构建代码,以便创建fetch 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. } }); } :尝试执行此操作应始终导致错误。获取该代码的唯一方法是调用fetch.get();enter image description here

  

每个Request对象都设计为使用一次。尝试重用它们会引发错误。