JavaScript for HTTP post XML with authentication

Go To


I want to implement something like curl --digest -u username:password -d "mydata" with Javascript as a chrome plugin. Here is my code.

<script src=""></script>
<script type='text/javascript' src="jquery.base64.js"></script>
<script language="javascript">

      function setHeader(xhr) {
          xhr.setRequestHeader('Authorization', $.base64Encode('username:password'));

        type: "POST",
        url: "",
        data: "<Phone><Data>mydata</Data></Phone>",
        xhrFields: {
           withCredentials: true
        crossDomain: true,
        beforeSend: setHeader,
        success: function (data,status,xhr){
          //do something

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.

2012-04-04 19:43
by Hollando Romi
If they don't support CORS (OPTIONS), there is nothing you can do about it - SLaks 2012-04-04 19:46


Did you try:

xhr.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password) //usage: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==


2012-04-04 19:49
by Steve
I tried. The same. OPTIONS instead of POST is sending - Hollando Romi 2012-04-04 21:54