0

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.

isherwood
  • 58,414
  • 16
  • 114
  • 157
dieguit
  • 11
  • 1
  • 4
  • By avoiding comma splices you've surpassed 90% of native English-speaking programmers already. :-) Welcome to SO. – isherwood Feb 24 '15 at 14:48
  • possible duplicate of [How to use Basic Auth and Jquery and Ajax](http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax) – nderscore Feb 24 '15 at 15:05

2 Answers2

1

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.

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
  • Thanks for the answer, the content type is set to text/plain. I will check about the error handler later =) – dieguit Feb 24 '15 at 15:16
0

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){}
}); 
mondjunge
  • 1,219
  • 14
  • 24
  • Thanks for the answer, i tried what you say and it still asks for user and pass. – dieguit Feb 24 '15 at 15:13
  • Than you should explore how the site you connect with expects the login data. Sometimes you need a GET request with pass and user as URL Parameter. Some APIS expect credentials in JSON. It is hard to tell from here how your desired REST Service works. I only pointed out, that passing pass and user as ajax options is wrong. – mondjunge Feb 24 '15 at 16:27