0

When i open using the Google chrome browser this url http://www.site.nl/a/b/c/d/e?niss=f, it first ask for username password:

enter image description here

Manually when i put the username password i get some data.

Now, using jQuery how can i submit that username password? following is not working, always after submit its failing with "401 unauthorized"

$(document).ready(function() {  
  $.ajax({
    type: "GET",
    url: "http://www.site.nl/a/b/c/d/e?niss=f",
    dataType: 'json',
    async: false,
    headers: {
      "Authorization": "Basic Username1:password1234"
    },
    data: '{ "test" }',
    success: function (data, textStatus, jqXHR){
      console.log(data, textStatus, jqXHR);
    }
  });  
});

1 Answers1

0

Please try following code

$(document).ready(function() {  
  $.ajax({
    type: "GET",
    url: "http://www.site.nl/a/b/c/d/e?niss=f",
    dataType: 'json',
    async: false,
    data: '{ "test" }',
    beforeSend: function(xhr)
    {
      xhr.setRequestHeader('Authorization', make_base_auth("Username1", "password1234"));
    },
    success: function (data, textStatus, jqXHR){
      console.log(data, textStatus, jqXHR);
    }
  });  
});

function make_base_auth(user,password)
{
  var tok = user + ':' + password;
  var hash = btoa(tok);
  return "Basic " + hash;
} // ends
Saurabh
  • 776
  • 1
  • 5
  • 15
  • Its still same failure. please see the failure details, check line 10: http://paste.ubuntu.com/16356592/ –  May 11 '16 at 07:36
  • `Response for preflight has invalid HTTP status code 401` - Always failing, can you please tell why? basically it works and i get XML data while using from web browser –  Jun 21 '16 at 06:36
  • Try specifying content-type in header and check, If you have access of domain then check if CORS is enabled or not – Saurabh Jun 22 '16 at 06:11