在JQuery中使用AJAX的基本授权 [英] Use of basic authorization in JQuery ajax

查看:131
本文介绍了在JQuery中使用AJAX的基本授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建Ajax调用如下:

I am creating an ajax call as follows:

beforeSend: function(xhr) { 
    xhr.setRequestHeader("Authorization", make_base_auth(username, password)); 
},

生成的HTTP请求头中包含

The resultant HTTP request header contains

Access-Control-Request-Headers: authorization

当我发出直接请求头包含正确的浏览器相同的请求

When I issue the same request from the browser directly the request header correctly contains

Authorization: Basic ZG<etc>

综观JQuery的程序, jQuery的-1.20.2 setRequestHeader 正确添加标题名称和值对头部的缓存列表。

Looking at the JQuery routines, jquery-1.20.2, the setRequestHeader is correctly adding the header name and value pair to the cached list of headers.

Ajax调用抛出一个错误,因为授权失败的结果。 (ZGetcetc比赛是我在成功地手动尝试看看)

The AJAX call throws an error as a result of the authorization failing. (ZGetcetc matches what I see in the successful manual attempt)

推荐答案

我不知道这是否是一个可行的选择你;升级你的jQuery。

I am not sure if this is a viable option for you; upgrade your jQuery.

Newever jQuery有传递基本身份验证用户名和密码作为Ajax调用的一部分的功能​​,如:

Newever jQuery has the ability to pass the basic auth username and password as part of the ajax call, e.g.

$.ajax
({
  type: "GET",
  url: "index.php",
  dataType: 'json',
  async: false,
  username: "YOUR-USERNAME-HERE",
  password: "YOUR-PASSWORD-HERE",
  data: '{ "DATA-HERE" }',
  success: function (data) {
     // handle success data
  }
});

源: http://api.jquery.com/jQuery.ajax/

另一种选择:

beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic "+ Base64.encode(username +':'+ password));
},

这篇关于在JQuery中使用AJAX的基本授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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