I want to implement something like curl --digest -u username:password -d "mydata" http://example.com/push with Javascript as a chrome plugin. Here is my code.
<html>
<head>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type='text/javascript' src="jquery.base64.js"></script>
<script language="javascript">
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', $.base64Encode('username:password'));
}
$.ajax({
type: "POST",
url: "http://example.com/push",
data: "<Phone><Data>mydata</Data></Phone>",
xhrFields: {
withCredentials: true
},
crossDomain: true,
beforeSend: setHeader,
success: function (data,status,xhr){
//do something
}
});
</script>
</body>
</html>
This is a cross domain POST with HTTP Basic Authentication. Because of CORS, chrome browser instead of POST sends an OPTIONS message challenge for originate. If I changed url to same domain, it works. If I disabled authentication, it also works. But I have to POST to the other domain, which is not under my control, that means I cannot use solutions like easyXDM. They also require HTTP Basic Authentication. I have been looking for solution for a week. Any hint is appreciated.
Did you try:
xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) //usage: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Reference: http://www.w3.org/Protocols/HTTP/1.0/spec.html#BasicAA
OPTIONS
), there is nothing you can do about it - SLaks 2012-04-04 19:46