您对异步代码的执行顺序感到困惑。
正如它所写的,
var assign = Request({
url: assignurl,
在你的回调函数执行提供assignurl的行之前执行:
assignurl=urls.URL('http:// ...
通过调用Request() constructor, (var assign = Request({),您已将url 分配为当时存在的assignurl。在该点之后更改变量assignurl 对assign Request 没有影响。因此,您要么需要更改assign.url,要么在获得可用 URL 后创建请求。
注意:当您调用Request 构造函数时,url 参数必须存在且有效。因此,我假设您的问题在说明错误时是不正确的:
RequirementError: The option "url" is invalid.
仅在fetch 的onCompletion 处理程序中发生。当您使用无效的assignurl 执行var assign = Request({url:asignurl 时应该会发生这种情况。
您可以通过多种方式更改代码以使用您现在在为assign 创建的Request 中拥有的URL(来自fetch onComplete 回调)。您使用哪种方法取决于您是否要在代码中的多个位置使用与 assign 非常相似的请求。
一个可能的改变是将assign 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 的onComplete 处理程序中的fetch.get();:尝试这样做总是会导致抛出错误。获得该代码的唯一方法是已经调用了fetch.get(); 和each Request can only be used once:
每个请求对象都设计为使用一次。尝试重用它们会引发错误。