How to send my login and password via $.ajax

Asked

Viewed 1,367 times

3

Hello, I have a link where it opens a modal window (as if it were an "Alert") to enter the login and password. I am using jQuery, the $.ajax{} function to be more specific. I would like to know how to send my login and password in its header. What type of command should I use? Authentication? How?

3 answers

5


In the documentation of . ajax() find some options.

You can use the beforeSend where they advise to set the headers. Example:

$.ajax({
    type: "POST",
    beforeSend: function (request) {
        request.setRequestHeader("Chave", 'valor');
    },

Or you can use the headers. Example:

$.ajax({
    url: 'meuSite.com/pasta',
    headers: { 'meu-header': 'valor ou senha' }
});

1

$.ajax({
    url:'link.php',
    username:'USR',
    password:'PWD', 
    success: function(completo){
        $('#resultado').html(completo);
    }
});

Use this if the authentication is actually HTTP, otherwise set the values in a field data:.

0

Hi, you can use this:

var username = $("#username").val();
var password = $("#password").val(); 

$.ajax({
    url: "teste.php",
    headers: {"Header-Teste": username, "Header-Teste2": password},
    success: function(data) { alert('Successo!' + data); }
    error: function () { alert('Erro!'); },
});
  • How can I make him identify the login and password? Because it asks 2 (user and password) and with these codes it seems to me that only for me to put, for example "user"

  • I didn’t get it right, but I edited the answer and put 2 values in the header, that’s it ?

  • Yes, more or less so. I would just like to know how to do it entered the login in the "login" and password field in the "password". Because he has to have the authorization to enter the admin panel and to have authorization he needs to inform this login and password

  • take a look here: http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax

  • added to Cód. 2 variables with the value of the fields...

  • With this I can be logging into an external system that asks for login and password? I have meusite.com and wanted to log in to your site.com would work? authenticates?

  • take a look here: http://www.html5rocks.com/en/tutorials/cors/

  • I read it, I just don’t understand how it does authentication

  • Hello, if it’s HTTP set in ajax the username : and password: which are fields for HTTP authentication.

  • They ask for my token, I inform them to authenticate only that returns me in Chrome error 401 Unauthorized

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.