Answer the question
In order to leave comments, you need to log in
CORS + Basic Authentication not working in most browsers
I'm trying to get JSON from an external domain. Everything would be fine, but it is protected using Basic Authentication, which for some reason complicates access for many browsers.
It is not possible to freely rotate the external domain, you can configure it, but it is administered on the client side and there is no way to constantly pull it. Therefore, I had to create a test bench in a vacuum.
There is the following code on the external domain:
<?php
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://clientdomain");
?>
{ "result": "ok" }
$('button').click(function(){
$.ajax({
url: 'http://overestimated.info/script.php',
type: 'GET',
crossDomain: true,
dataType: 'json',
username: 'user',
password: 'user',
success: function(x, status){
console.log(JSON.stringify(x));
},
error: function(x, status, error){
console.log(status);
console.log(error);
}
});
});
xhrFields: {
withCredentials: true
}
или
beforeSend: function(xhr){
xhr.withCredentials = true;
},
Answer the question
In order to leave comments, you need to log in
In general, I tend to think that this is a bug in jQuery. I wrote a code for CORS with Basic Auth "hands" without jQuery, it works at least in Fx 10 and 17 and in Opera 12.10
The code is as follows:
on the client −
<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CORS testing</title>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("1").addEventListener("click", function() {
var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
document.getElementById("2").textContent = xhr.responseText;
};
xhr.open("GET", "http://dev.merlin-vrn.tk/cors/api.php", true);
// xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk");
xhr.send();
}, false);
}, false);
</script>
</head><body>
<button id="1">Click me</button>
<div id="2"></div>
</body></html>
<?php
if (isset($_SERVER['HTTP_ORIGIN']))
header("Access-Control-Allow-Origin: $_SERVER[HTTP_ORIGIN]");
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: $_SERVER[HTTP_ACCESS_CONTROL_REQUEST_METHOD]");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: $_SERVER[HTTP_ACCESS_CONTROL_REQUEST_HEADERS]");
} else {
header('WWW-Authenticate: Basic realm="API"');
header("Content-Type: application/json");
if (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'] == "testuser" && isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_PW'] == "testpassword") {
header('HTTP/1.1 200 OK');
echo '{"result": "ok"}';
} else {
echo '{"result": "noauth"}';
}
}
file_put_contents("api-log.txt", "$_SERVER[REQUEST_METHOD] $_SERVER[REQUEST_URI] $_SERVER[SERVER_PROTOCOL]\n", FILE_APPEND);
file_put_contents("api-log.txt", print_r($GLOBALS, true), FILE_APPEND);
file_put_contents("api-log.txt", "\n\n", FILE_APPEND);
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question