Expand my Community achievements bar.

SOLVED

Seeking help verifying SAP portal login in Adobe form using JavaScript, need alternative solutions

Avatar

Level 1

When a button is clicked in my Adobe Interactive Form, I aim to verify user credentials for the SAP portal using JavaScript, but I'm not receiving the expected status back. Here's the code I'm using:

when button CLICK event in the adobe digital form 

var cPortalUrl = "https://nspek.com:50001/irj/portal?j_user= <userid>&j_password=<password>";

var cStatus;

var pingportalstatus =
function PortalPing (cPortalUrl){
    var xmlHttp = new XMLHttpRequest();

xmlHttp.onreadystatechange = function()  

     { 

          cStatus =  xmlHttp.status;

     };


    xmlHttp.open("GET", cPortalUrl, false);
    xmlHttp.send();

return xmlHttp.status;
};

Any advice on why cStatus isn't being populated would be appreciated.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi, 

 

I think the issue is that the xmlHttp.status property is not guaranteed to be populated synchronously in your code due to the asynchronous nature of XMLHttpRequest. It's populated asynchronously when the state of the XMLHttpRequest changes. 

You can learn more here about how this works: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status.

 

I would use FETCH instead of XHR due to performance reasons. It would be something like this:

var cPortalUrl = "https://nspek.com:50001/irj/portal?j_user=<userid>&j_password=<password>";

function PortalPing(cPortalUrl, callback) {
    fetch(cPortalUrl)
        .then(function(response) {
            if (response.ok) {
                callback(response.status);
            } else {
                callback(response.status);
            }
        })
        .catch(function(error) {
            console.error("Fetch error:", error);
            callback(null); // Pass null status to callback to indicate error
        });
}

// Example usage
PortalPing(cPortalUrl, function(status) {
    if (status !== null) {
        console.log("Status:", status);
    } else {
        console.log("Error occurred during fetch.");
    }
});


Hope this helps.



Esteban Bustamante

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi, 

 

I think the issue is that the xmlHttp.status property is not guaranteed to be populated synchronously in your code due to the asynchronous nature of XMLHttpRequest. It's populated asynchronously when the state of the XMLHttpRequest changes. 

You can learn more here about how this works: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status.

 

I would use FETCH instead of XHR due to performance reasons. It would be something like this:

var cPortalUrl = "https://nspek.com:50001/irj/portal?j_user=<userid>&j_password=<password>";

function PortalPing(cPortalUrl, callback) {
    fetch(cPortalUrl)
        .then(function(response) {
            if (response.ok) {
                callback(response.status);
            } else {
                callback(response.status);
            }
        })
        .catch(function(error) {
            console.error("Fetch error:", error);
            callback(null); // Pass null status to callback to indicate error
        });
}

// Example usage
PortalPing(cPortalUrl, function(status) {
    if (status !== null) {
        console.log("Status:", status);
    } else {
        console.log("Error occurred during fetch.");
    }
});


Hope this helps.



Esteban Bustamante

Avatar

Level 1

This works! How did you learn that - got experience creating digital forms?

Avatar

Community Advisor

I'm glad to hear it worked! This is more related to Javascript skills. If you're interested in furthering your JavaScript skills, reviewing asynchronous and synchronous concepts, especially in the context of handling HTTP requests, can be very helpful.

Cheers!



Esteban Bustamante

Avatar

Administrator

@Borowski I hope the AEM community has been helpful. We look forward to your return as either a learner or a contributor. The community grows with SMEs like you. Invite your AEM peers to contribute too. Happy AEM learning!



Kautuk Sahni