I configures my Jetty server to allow cross domain http requests (allowedOrigins = *) and also to allow cross domain authentication (allowCredentials=true) using its CrossOriginFilter. Cross domain http requests without authentication requirement work ok. Now when it comes to http calls that require authentication It doesn't work out using JQuery. I use the following code and followed this example: http://www.aswinanand.com/2009/01/http-basic-authentication-using-ajax/
function login(username, password) {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: "json",
url: url,
beforeSend: function(xhr) {
var base64 = Base64.encode(username + ":" + password);
xhr.setRequestHeader("Authorization", "Basic " + base64);
xhr.withCredentials = true;
},
error: function(data){
alert("error");
},
success: function(data){
alert("success");
}
});
In HTTPFox i see the following request to the server:
OPTIONS /login HTTP/1.1
...
Access-Control-Request-Method GET
Access-Control-Request-Headers authorization,content-type
The server responds with a
HTTP/1.1 204 No Content
...
Allow OPTIONS,GET,HEAD
I also used the options below, which doesnt make a difference.
$.ajax({
...
username: username,
password: password,
...
}
The error function always fires. Anybody an idea what the problem could be?