javascript - 如何使用 jQuery 调用需要基本身份验证的 JSON Web 服务?

标签 javascript jquery json

我在 javascript 方面有点新手,但我正在尝试调用一个需要使用 jQuery(或任何真正有效的东西)进行基本身份验证的 JSON Web 服务。

我无法在 Google 上找到任何真正的答案。我正在尝试做的事情可能吗?

最佳答案

您需要设置适当的请求 header 以传递凭据。例如参见 here .

$.getJSON({
    'url': 'http://host.com/action/',
    'otherSettings': 'othervalues',
    'beforeSend': function(xhr) {
        //May need to use "Authorization" instead
        xhr.setRequestHeader("Authentication",
            "Basic " + encodeBase64(username + ":" + password)
    },
    success: function(result) {
        alert('done');
    }
});

仅供引用,我在 Google 中搜索了 jquery post with basic auth,这是第一个链接。

关于javascript - 如何使用 jQuery 调用需要基本身份验证的 JSON Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6025764/

相关文章:

javascript - jQuery 中有 .findUsingParent() 函数吗?

javascript - 忽略 if 语句的 PHP 函数

Java JSON POST 格式不正确

javascript - 如何根据数组的结果更改html部分的背景颜色

javascript - 多系列数据 Highcharts 线

javascript - 手动显示和隐藏 jQuery ui 工具提示

javascript - 为什么 mongodb find() 从来没有得到我的回调

jquery - 如何检查 cookie 是否已使用 jQuery 设置?

javascript - Windows 小工具 : How do i get json from a webpage and convert to a series of arrays with javascript?

json - 使用\COPY 将带有 JSON 字段的 CSV 加载到 Postgres