I have some RESTful WebServices under Apache Tomcat Server with BASIC Authorization.
I wanna serve this services to javascript clients with ajax calls. I'm trying to access it with the following code:
<!DOCTYPE html>
<html>
<body>
<p><input type="button" value="Call API!" onClick="callApi();"/></p>
</body>
<script language="text/javascript"></script>
<script>
function callApi(){
try {
var params = "latitude=-25.42&longitude=-49.27";
var xhr = new XMLHttpRequest();
xhr.open("POST"
, "http://192.168.0.12:8080/project/api/retrieveFullAddress/json"
, true /*async*/);
var basicAuth = btoa("email@domain.com:Password123");
xhr.setRequestHeader("Authorization", "Basic " + basicAuth);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
//xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
}
xhr.send(params);
} catch (e) {
alert(e.message);
}
}
</script>
</html>
However, the server is rejecting my POST call, returning "401 Unauthorized". Firebug shows me the following set of server's responses:
request header
OPTIONS /project/api/retrieveFullAddress/json HTTP/1.1
Host: 192.168.0.12:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Origin: null
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
response header
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Allow: OPTIONS,POST
Last-Modified: Dom, 17 Nov 2013 10:30:35 BRST
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization, authorization
Vary: Accept
Content-Type: application/xml
Content-Length: 998
Date: Sun, 17 Nov 2013 12:56:13 GMT
request header
POST /project/api/retrieveFullAddress/json HTTP/1.1
Host: 192.168.0.12:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Authorization: Basic dGVmZGljYWRvQGVhc3l0ZWYuY29tLmJyOlBhc3N3b3JkMTIz
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 32
Origin: null
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
response header
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
WWW-Authenticate: Basic realm="Restrict Area"
Content-Type: text/html;charset=utf-8
Content-Length: 951
Date: Sun, 17 Nov 2013 12:56:13 GMT
If I uncomment the line xhr.withCredentials = true;
firebug shows me that only the OPTIONS command is executed, without running the POST command. The service it's really not executed.
What can I do to solve this problem? What is going on?
Thanks in advance.