jquery ajax get with authentication 1

jquery ajax get with authentication

var username="username_here";
var password="password_here";
$.ajax({
  type: "GET",
  url: "myapi.php",
  dataType: 'json',
  headers: {
    "Authorization": "Basic " + btoa(username + ":" + password)
  },
  success: function (result){
      console.log(result)
  }
});

Here is what the above code is Doing:
1. We are using the jQuery library to make an AJAX request to the API.
2. We are passing the username and password to the btoa() function which returns a base64 encoded ASCII string.
3. We are setting the Authorization header to the base64 encoded string.
4. We are setting the dataType to json.
5. We are setting the success function to log the result to the console.

Similar Posts