How to send my login and password via $ .ajax

3

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

    
asked by anonymous 13.06.2014 / 00:31

3 answers

5

In the .ajax () documentation you will find some options.

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

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

Or you can use headers . Example:

$.ajax({
    url: 'meuSite.com/pasta',
    headers: { 'meu-header': 'valor ou senha' }
});
    
13.06.2014 / 01:03
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 data: field.

    
13.06.2014 / 02:40
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!'); },
});
    
13.06.2014 / 01:03