tags:

views:

42

answers:

2

Is it possible to use jQuery's $.post function and have the cookies set in the browser? I'm trying to set it up so that it is possible for my users to be automatically logged in to a certain site by sending POST variables.

+2  A: 

Yes, you can $.post to a server-side page which handles the session that sets the cookies and all that business.

meder
A: 

To answer your question, yes, you can make an ajax call and have the server set the necessary session/authentication cookies.

You could also POST via $.ajax which may give you a little more (but maybe not needed) control over the event. For example:

$.ajax({
    async: false,
    cache: false,
    type: 'post',
    dataType: 'json',  // json...just for example sake
    data: ({
        'username': $('#Username').val(),
        'password': $('#Password').val()
    }),
    url: '/Login/Authorize.php',
    success: function (data) {
        // retrieve a success/failure code from the server
        if (data === '1') {  // server returns a "1" for success
            // success!
            // do whatever you need to do
        } else {
            // fail!
        }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        // something went wrong with the request
        alert(XMLHttpRequest.responseText);
    }
});
uhleeka