dieguit
dieguit

Reputation: 11

JQuery cross-site request with credentials

As the tittle says, i'm trying to access a cross-site REST service, using JQuery with CORS. The problem are the credentials. I followed some answers from this site, but it seems my code is not sending any validaton data since it asks for the user and pass opening a window. I'll show some code so you see it:

JQquery:

$.ajax({
    username: 'user',
    password: 'pass',
    url: url,
    dataType: 'json',
    data: {},
    crossDomain: true,
    xhrFields: {
          withCredentials: true
    },
    success: function(data) {},
    error: function(e){}
}); 

When the AJAX request is made, as i said, a window appears asking for username and password. This shoud not appear, since the user and pass are in the AJAX request! If i put the user and pass it works, so the validaton works fine on destination server.

Thanks for your time! Sorry about my non-native english writing =D.

Upvotes: 0

Views: 2110

Answers (2)

Bud Damyanov
Bud Damyanov

Reputation: 31839

For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server. Also the error handler is not called for cross-domain script and cross-domain JSONP requests, so you wont be able to handle it. This is an Ajax Event.

See here for details and another great article on how to make cross-domain requests with CORS here.

Upvotes: 1

mondjunge
mondjunge

Reputation: 1239

There is no username and passwort options, you should put this in the request data, like this:

$.ajax({
    url: url,
    dataType: 'json',
    data: {
        username: 'user',
        password: 'pass'
    },
    crossDomain: true,
    xhrFields: {
          withCredentials: true
    },
    success: function(data) {},
    error: function(e){}
}); 

Upvotes: 0

Related Questions