jQuery的基本身份验证错误 [英] Jquery basic authentication error

查看:132
本文介绍了jQuery的基本身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手jQuery的。 我试图发送使用jQuery的基本身份验证的要求。这是我做的。

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));
          }
});

但我的问题是,我没有得到任何feedack。看起来无论成功或错误方法被调用。

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

我在想什么?帮助将AP preciated。

What am i missing? Help would be appreciated.

推荐答案

该错误是说,有没有一种方法被称为连接codeBase64定义。

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

许多浏览器有一个内置的转换,从ASCII码为Base64称为BTOA:

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

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

如果你是支持老版本浏览器,请确保有一个base64 polyfill,比如那个在<一个href="http://ww.w.i$c$csnip.com/search/javascript%20dark%20$c$cs/45">http://ww.w.i$c$csnip.com/search/javascript%20dark%20$c$cs/45

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

这篇关于jQuery的基本身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆