Neelesh
Neelesh

Reputation: 3693

Jquery basic authentication error

I am a newbie to Jquery. I am trying to send a request with basic authentication using jquery. This is what i do.

$.ajax({
          type: "GET",
          url: domain,
          dataType: 'json',
          success: function(){
            alert('Thanks for your comment!'); 
          },
          error:function(){
            alert('here');
          },
          beforeSend: function(xhr){ 
            xhr.setRequestHeader('Authorization', 'Basic ' + encodeBase64(username + ":" + password));
          }
});

But my problem is I dont get any feedack. Looks like both the success or the error method is called.

On further debugging i get

Uncaught ReferenceError: encodeBase64 is not defined

What am i missing? Help would be appreciated.

Upvotes: 1

Views: 8196

Answers (1)

steveukx
steveukx

Reputation: 4368

The error is saying that there is no method called encodeBase64 defined.

Many browsers have a built-in conversion from ascii to base64 called btoa:

xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));

If you are supporting older browsers, make sure that there is a base64 polyfill, such as the one at http://ww.w.icodesnip.com/search/javascript%20dark%20codes/45

Upvotes: 7

Related Questions