javascript - How do you call a JSON web service, that requires basic authentication, using jQuery? - Stack Overflow

I am somewhat novice at javascript, but I am trying to call a JSON web service that require basic authe

I am somewhat novice at javascript, but I am trying to call a JSON web service that require basic authentication using jQuery (or anything that would work really).

I've not been able to come up with any real answers on Google. Is what I am trying to do possible?

I am somewhat novice at javascript, but I am trying to call a JSON web service that require basic authentication using jQuery (or anything that would work really).

I've not been able to come up with any real answers on Google. Is what I am trying to do possible?

Share Improve this question asked May 17, 2011 at 2:56 aceintheholeaceinthehole 5,22211 gold badges39 silver badges54 bronze badges 3
  • See here for an example: stackoverflow.com/questions/671042/… – onteria_ Commented May 17, 2011 at 2:59
  • +1: great question; explores an aspect of Ajax I had never even thought about. – Roy Tinker Commented May 17, 2011 at 4:14
  • Is the JavaScript executing on a page that is hosted at the same domain as the web service? – Crescent Fresh Commented May 17, 2011 at 4:46
Add a comment  | 

3 Answers 3

Reset to default 10

You will need to set the appropriate request header to pass the credentials. For example see 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');
    }
});

FYI I searched Google for jquery post with basic auth and this was the first link.

Here's the way to do it with jQuery for your copy and pasting needs:

$.ajax({
    url: "/somewhere",
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + window.btoa(username + ":" + password));
    },
    success: function(result) {
        console.log(arguments);
    }
});

Simple.

In asp.net create a reference to the service. Create a web page (with no UI) and make multiple methods in the code behind that are "wrappers" for that service (in C#/VB.NET). Decorate the methods with [WebMethod] and set the WebMethod's Serialization to JSON.

Alternatively you can do the same with any other language (pearl, php, whatever) by making a wrapper for the json web service.

The reason you need that wrapper is because that way you avoid the cross-site scripting... limitations in JS. Also if your page is served over HTTPS, than your JS calls to your wrapper will also be over HTTPS thus not having to worry about security.

Your JS wrapper will be taking care of negotiating the connection, authentication, etc...

The javascript within your other pages can post to the methods in this page as:

$.post('pagename/method_name', {data:value}, callback(){

});

or $.post, $.get, $.ajax... will all work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1738759203a4073463.html

相关推荐

  • SVN服务器安装 - Windows系统

    文章目录 前言1 下载安装1.1 下载安装包1.2 软件安装1.3 异常处理 2 仓库创建3 权限分配 前言 Windows系统下SVN服务器搭建及遇到相关问题和处理方法记录 1 下载安装 1.1 下载安装包 Windows系统中的S

    1小时前
    00
  • Windows下配置Golang开发环境,并安装配置GoLand IDE

    作者:非妃是公主 专栏:《Golang》 博客地址:https:blog.csdnmyf_666 个性签:顺境不惰,逆境不馁

    1小时前
    00
  • 开发体育直播系统后台权限设计实践分享|ThinkPHP 技术栈落地案例

    今天我们分享的是一套由 东莞梦幻网络科技 自研的体育直播源码,在 ThinkPHP + MySQL 技术栈的加持下,后台权限系统如何从0到1落地,并支撑整个平台稳定运行。一、整体架构设计代码语言:html复制用户端(APPH5P

    1小时前
    20
  • 如何使用python查询Prometheus监控数据

    一.环境准备软件库包版本python3.8prometheus-api-client0.5.7二.安装步骤代码语言:python代码运行次数:0运行复制pip3 install prometheus-api-client默认安装最新版本的p

    1小时前
    20
  • windows 配置 upx

    ​1、下载:2、解压:解压后​3、配置环境变量,右键我的电脑-——》属性——》高级属性:在Path中添加: D:ProgramFileupx-5.0.0-win644、验证cmd中输入:代码语言:txt复制upx --version

    1小时前
    20
  • 怎么用html写出哆啦A梦?

    用HTML和CSS来画哆啦A梦(Doraemon)是一项有趣且具有挑战性的任务。虽然HTML和CSS主要用于网页布局和样式,但通过巧妙的组合和定位,可以创建出相对简单的图形和图案。下面是一个简单的示例,展示了如何用HTML和CSS绘制哆啦A

    1小时前
    00
  • 流固耦合:基本概念、适用软件及 Abaqus 与 Powerflow 的协同仿真

    在工程和科学研究的诸多领域,流固耦合现象广泛存在且对系统性能有着关键影响。理解流固耦合的本质及其应用,对于优化设计、保障安全运行意义重大。同时,借助专业的流固耦合软件,能够更高效地对相关问题进行分析与模拟。接下来,让我们深入探究流固耦合的奥

    1小时前
    00
  • MySQL8.4 Enterprise安装Firewall及测试

    参考:.4enfirewall.html1.首先执行安装SQL,路径在baseshare目录下代码语言:javascript代码运行次数:0运行复制cd u01mysql3308baseshare[root@mysql8_3

    1小时前
    00
  • Oracle linux 8 二进制安装 MySQL 8.4企业版

    使用命令ldd --version ldd 检查,确定MySQL 8二进制包版本代码语言:javascript代码运行次数:0运行复制[root@mysql8_3 ~]# ldd --version ldd安装libaio代码语言:java

    1小时前
    00
  • 电脑分区后进不了系统怎么办,修复教程

    电脑分区后进不了系统,可能的原因有多种,包括分区操作不当导致系统文件丢失或损坏、BIOS设置错误、引导文件未正确配置等。针对这些问题,可以尝试以下解决方法&#xff1

    58分钟前
    00
  • Go项目实战

    上节课我给大家介绍了怎么给Go项目做单元测试的规划,当然这里仅限于跟咱们课程里的实战项目一样分层架构设计做的还可以的项目哦,要是所有逻辑都耦合在Controller里,那这个规划就不适用了。。。,所有逻辑都耦合在Controller里还做个

    58分钟前
    00
  • 计算机开机长鸣报警,电脑开机报警,详细教您电脑开机一直长鸣报警怎么办

    朋友们在使用电脑的过程中,经常会碰到开机一声长鸣,然后一直响个不停,这到底是怎么回事呢? 其实这个现象的产生一般是因为内存的问题,那用户遇到电脑开机一直

    57分钟前
    00
  • 10个 DeepSeek 神级提示词,建议收藏!

    在当下人工智能飞速发展的时代,DeepSeek 作为一款功能强大的 AI 工具,能够帮助我们实现各种创意和需求。然而,要充分发挥它的潜力,掌握一些巧妙的提示词至关重要。今天,就为大家精心整理了 15 个 DeepSeek 神级提示词,涵盖多

    44分钟前
    00
  • 电脑开机会默认一件GHOST

    关于电脑开机会自己重装系统 前段时间电脑一开机就遇到会自己ghost的问题,而且一直再重复同样的操作,我点击restart的时候到开启页面又会自动ghost,而且此页面停留

    30分钟前
    00
  • AlignRAG:浙江大学提出的可泛化推理对齐框架,助力 RAG 系统解决推理失配问题

    近年来,检索增强生成(Retrieval-Augmented Generation, RAG)成为知识驱动文本生成的核心范式。然而,现有的 RAG 系统在推理过程中常常出现“推理失配”问题,即模型的推理路径与检索到的证据不一致,导致生成内容

    30分钟前
    00
  • 国产车载通信测试方案:车规级CAN SIC芯片测试技术解析

    随着智能网联汽车的快速发展,车辆内部电子控制单元(ECU)数量激增,动力总成、高级驾驶辅助系统(ADAS)、车身控制等功能对车载通信网络的稳定性与速率提出了更高要求。传统CAN FD总线在复杂拓扑中面临信号振铃、通信速率受限(实际速率通常低

    26分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    18分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    14分钟前
    00
  • 深度学习在DOM解析中的应用:自动识别页面关键内容区块

    爬虫代理摘要本文介绍了如何在爬取东方财富吧()财经新闻时,利用深度学习模型对 DOM 树中的内容区块进行自动识别和过滤,并将新闻标题、时间、正文等关键信息分类存储。文章聚焦爬虫整体性能瓶颈,通过指标对比、优化策略、压测数据及改进结果,展示了

    9分钟前
    10
  • 什么是docker?它是如何工作的?

    想象一个场景,你要部署一个服务,然后它对环境有很多依赖,不同的操作系统又是不同的需求,而且还可能遇到有些源不能使用,又得一番折腾,折腾完上线后,假设要在新的环境再来一套,又得再来一遍。那么有没有什么办法可以解决呢?有办法,docker就是干

    7分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信