0

I am using two function one function having ajax calls i.e

function fetchProjectAreaConfigStateIds(projectAreaContextId) {
paConfigStateIdList = [];
var getUrl = widgetLink+ "/ccm/restcaller/"
        + projectAreaContextId;
var jqXHR = $
        .ajax({
            method : 'GET',
            url : getUrl,
            paId : projectAreaContextId,
            headers : {
                'Accept' : 'text/json',
                'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8'
            },
            success : function(data) {

                stateid = (/c=(.+)($|&|")/.exec(data[this.paId].imagePath))[1];
                console.log("State Id :" + stateid);

                return stateid;

            },
            error : function(jqXHR, textStatus) {

                console
                        .error('#ERROR: fetchProjectAreaStateIds: error retrieving enumerates for PA '
                                + _paId + '.');
            }
        });

        } 

and other function which does not have any ajax calls rather than its doing xml parsing i.e

 xhttp.open("GET", random , true);
 xhttp.send();
 function myFunction(xml) {

 xmlDoc = xml.responseXML;
 x = xmlDoc.getElementsByTagName('contextId');
 a = xmlDoc.getElementsByTagName('name');

for(i=0;i<a.length;i++)
{
    y = x[i].childNodes[0].nodeValue;
    if((a[i].childNodes[0].nodeValue)===(actualPA)){
    console.log("uuid for pa"+y);

    }

   }

  }

Now my main function where i am calling both the function

  $(document).ready(function() {
  console.log("Starting DefectFix Widget Script...");


  //onLoad: 1.Get pAID of tools/Component

  xhttp.onreadystatechange = function() 
  {
if (this.readyState == 4 && this.status == 200) {
    myFunction(this);
}
};
  if (parentLink.includes(templink)) {
        projectAreaContextId = ContextKeyValue; 
        console.log("y is "+projectAreaContextId);
    }
   fetchProjectAreaConfigStateIds(projectAreaContextId);


   }
//global parameter
var projectAreaContextId;
var xmlDoc;
var ContextKeyValue;
var ContextKey;
var ProjectAreaKey;
var random =widgetLink+"/ccm/rpt/repository/workitem?fields=workitem/projectArea/*";
var xhttp = new XMLHttpRequest();

Now my problem list :

1.The excepted value of ProjectAreaContextId shall be equal to that of ContextKeyValue (which abc34r),but is coming undefined 2.Am i doing a wrong call in main function for xml parser function if yes please correct because that is the reason why i feel value of ProjectAreaContextId goes undefined

Since i am new to javascript, i really don't know how to handle this scenario i want Myfunction() to be executed first and rather than the function with ajax call, so that value of ProjectAreaContextId gets updated firstly then goes as an actual parameter fetchProjectAreaConfigStateIds(projectAreaContextId) Your help will really be appreciated also please let me know if my question is not clear, Thanks in advance,Stay Safe!!

Edit 1 :

 xhttp.open("GET", random , true);
 xhttp.send();
 function myFunction(xml) {

 xmlDoc = xml.responseXML;
 x = xmlDoc.getElementsByTagName('contextId');
 a = xmlDoc.getElementsByTagName('name');

 for(i=0;i<a.length;i++)
{
y = x[i].childNodes[0].nodeValue;
if((a[i].childNodes[0].nodeValue)===(actualPA)){
console.log("uuid for pa"+y);

}

}
    if (parentLink.includes(templink)) {
    projectAreaContextId = ContextKeyValue; 
    console.log("y is "+projectAreaContextId);
}
}
  • The entire `if (parentLink.includes(templink)) {` block and the `fetchProjectAreaConfigStateIds(projectAreaContextId);` call must be placed inside the asynchronous `onreadystatechange` callback, after the call to `myFunction`! – Bergi Apr 30 '20 at 07:34
  • @Bergi u mean something like my edit 1? – Rajat Krishnan Apr 30 '20 at 07:37
  • Putting it inside the `myFunction` should work as well, although that's not what I meant. – Bergi Apr 30 '20 at 07:38
  • @Bergi i am not really getting you what u mean by placing all the blocks and fetchProjectAreaConfigStateIds(projectAreaContextId); in asynchronous callabcks?Can uplease show – Rajat Krishnan Apr 30 '20 at 07:38
  • Do you know which one the `onreadystatechange` callback is? – Bergi Apr 30 '20 at 07:41
  • @Bergi can you also please reopen my question so that others can read it as well because i am new and i will not understand the solution which does not have reference with my code – Rajat Krishnan Apr 30 '20 at 07:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212835/discussion-between-rajat-krishnan-and-bergi). – Rajat Krishnan Apr 30 '20 at 07:43

0 Answers0