Forum Discussion

Re: JavaScript API_DoQuery failing to give the proper xml response

There is a very simple explanation and fix but let me answer first by proposing a metaphor. Your situation is very similar to ordering a meal at a restaurant and immediately complaining that you didn't received anything because you didn't wait for the order to be placed, cooked and served.

We normally think of a computer as executing one instruction after another with each instruction completing before the next instruction is started. When making network requests we have to modify this step by step model because when a network request is made it takes an unpredictable amount of time before the response is actually received.

When you make a network request using jQuery's $.get(), $.post(), $.ajax(), or $.getScript() methods what is returned is not the response but an object called a promise. A promise is like a claim check at the laundry or a number ticket at the bakery in that the promise is the mechanism by which you will eventually get your result when it become available.

One way to patch up your code is to make the function getAndSetList() return a promise. Here is a version of your code that makes url a parameter to getAndSetList() and more importantly returns a promise:

function getAndSetList(url) {
  getPerm();
  return $.get(url, {
    act: "API_DoQuery",
    query: "{66.IR.'last wk'}",
    clist: "66"
  });
}

var url = "blahblah";
getAndSetList(url).then(functioin(xml) {
  console.dirxml(xml);
  //your further code here
});

Note that I have no idea what getPerm() does as it does not appear to influence the function and you did not provide any further details.
No RepliesBe the first to reply