Related to this question Convert curl-command to php curl I'm trying to convert a curl-statement to an ajax request.
My request is working with the following curl-command:
curl --user XXXX:YYYY "URL"
But trying to use it in jquery ajax it is not working, where my code looks like:
$.ajax({
url : "URL",
type : 'GET',
dataType : 'json',
contentType : 'application/json',
username: "XXXX",
password: "YYYY",
success : function(data) {
console.log(JSON.stringify(data));
},
error : function() {
console.log("Cannot get data");
}
});
I also tried to set a beforeSend value, shown below:
$.ajax({
url : "URL",
type : "GET",
processData: false,
dataType : 'json',
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
data : JSON.stringify(text),
withCredentials : true,
beforeSend : function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("XXXX:YYYY"));
}
}).then(function(data) {
console.log('data', data);
}, function(err) {
console.log('err', err);
});
In both cases I get the same error:
XMLHttpRequest cannot load "URL". Response for preflight has invalid HTTP status code 401
Does anyone knows how to fix this?
UPDATE
The headers look like this:
General
Request URL: "URL"
Request Method:OPTIONS
Status Code:401 Unauthorized
Remote Address:***
Response Headers
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Connection:Keep-Alive
Content-Language:en
Content-Length:1061
Content-Type:text/html;charset=utf-8
Date:Wed, 10 Feb 2016 13:12:34 GMT
Expires:0
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache
Set-Cookie:JSESSIONID=CE27E7E84651D519BD99802B65C431EB; Path=/matchbox-webservice/; Secure
Strict-Transport-Security:max-age=31536000 ; includeSubDomains
WWW-Authenticate:Basic realm="Realm"
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,pt;q=0.2,nb;q=0.2,da;q=0.2
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:***
Origin:http://evil.com/
Pragma:no-cache
Referer:http://localhost:8080/templates/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36