Calling API with Javascript

Bear with me, I dont have a huge amount of experience with Javascript.
I have a form with 2 textbox called “inputUserName”, and “inputPassword”. I will have two buttons, one called “buttonLogin” and one called “buttonGetContract”.

When I click the buttonLogin, I need to post to an API on my backend website (ASP.NET MVC Core), and get a JSON Web Token so that when I click the buttonGetContract I can use that web token to get a json list of contracts back from an API call.

Here is some sample code I am reading but I am not sure how to transpose this into the NSB functions.

$(function() {
var store = store || {};
/*
 * Store JWT
 */
store.setJWT = function(data) {
    this.JWT = data;
}
/*
 * Login
 */
$("#frm-login").submit(function(e) {
    e.preventDefault();
    var data = $("#frm-login").serialize();
    data += '&action=login';
    $.post("api/login", data, function(data) {
        $("button").removeAttr("disabled");
        store.setJWT(data.JWT);
    }).fail(function(xhr, status, error) {
        alert("login failed. status: " + status + ", error: " + error);
    });
});
/*
 * Get protected resource
 */
var getProtectedResource = function(e) {
    e.preventDefault();
    $.ajax({
        url: "api/view/orders/10248",
        type: "GET",
        success: function(data, status, xhr) {
            var out = (data && typeof data === 'object') ? JSON.stringify(data) : data;
            alert(out);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("status: " + xhr.status + ", error: " + thrownError);
        },
        beforeSend: function(request) { // Set JWT header
            request.setRequestHeader('Authorization', 'Bearer ' + store.JWT);
         } 
    });
}
$("#btn-with-token").click(getProtectedResource);
});

I am trying to compare to the NSB Ajax function and also how to refactor this code to work better in AppStudion JS.

I think I should break

store.setJWT()

into a different function.

Are there different rules for writing JS within AppStore vs, JS in an html page?

Thanks

Do you mean AppStudio in the previous sentence?

Are you also using BASIC in this project?

Yes, AppStudio, dumb autocorrect!

Using JavaScript

The brief answer is that there are no special rules for JS in AppStudio projects. AppStudio compiles JS code verbatim when you deploy.

If the sample code works fine in AppStudio, there is no need to refactor it except for programming style. You might want to hit the eslint button for an analysis of that.