Cross site HTTP authentication in jQuery

I want to see if I can connect to a third site using HTTP authentication. Ideally, the browser will save the credentials. Unfortunately, this fails every time. Any help would be much appreciated, I am using j64 plugin for base64 which I tested to work.

So, two questions:

  • How to view HTTP status code?
  • Will this ultimately work in principle?

<script> $("#login_button").click(function() { var username = 'myFunUsername'; var password = 'myFunPassword'; $.ajax({ url: 'http://remote-site-with-http-auth.com/member_site', beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " + $.base64.encode(username + ":" + password)); }, success: function(data) { $("#label").text("Logged in, yay!"); } }).fail(function(){ $("#label").text("It didn't work"); }); }); 

Thanks!!

+4
source share
1 answer
 var user= 'myFunUsername'; var pwd= 'myFunPassword'; $.ajax({ url: 'http://remote-site-with-http-auth.com/member_site', crossDomain:true, username :user,// Keep the variable name different from parameter name to avoid confusion password:pwd, xhrFields: { /* YOUR XHR PARAMETERS HERE */} beforeSend: function(xhr) { // Set your headers }, success: function(data, textStatus, xhr) { alert(xhr.status);// The status code //Code for success } }).fail(function(){ //Fail code here }); 

For more information on options, see http://api.jquery.com/jQuery.ajax/

+2
source

Source: https://habr.com/ru/post/1414625/


All Articles