Implement REST API with Javascript


#1

Hello! I’m currently using an online IDE to create a mobile app. I want the app to be able to authenticate users using REST API from an external database.

I’ve written the login form using HTML:

<ons-page>
  <ons-toolbar>
    <div class="center">Login</div>
  </ons-toolbar>

  <div style="text-align: center; margin-top: 30px;">
    <p>
      <ons-input id="username" modifier="underbar" placeholder="Username" float></ons-input>
    </p>
    <p>
      <ons-input id="password" modifier="underbar" type="password" placeholder="Password" float></ons-input>
    </p>
    <p style="margin-top: 30px;">
      <ons-button onclick="login()">Sign in</ons-button>
    </p>
  </div>
  </ons-page>

To call to the Web API, I’m using Javascript:

var username = "Username";
  var password = "Password";
 
  
  function = login () {
      var request = new XMLHttpRequest ();
      request.open("GET", "https://xxx123.caspio.com/rest/v1/tables", false);
      request.setRequestHeader ("Authorization", "Bearer");
      request.send();

This is pretty much how far I’ve gotten. I’ve got a token endpoint and request endpoint, but I’m not quite sure how to use it yet. I also want to have error message in case the information a person has entered is incorrect.

Can I use if statements to implement error messages?

Thank you for the help!