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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6025764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:55:30  来源:igfitidea点击:

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

javascriptjqueryjson

提问by aceinthehole

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).

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

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

我一直无法在谷歌上想出任何真正的答案。我正在尝试做的可能吗?

回答by mellamokb

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 authand this was the first link.

仅供参考,我在谷歌上搜索过jquery post with basic auth,这是第一个链接。

回答by Philipp Gayret

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

以下是使用 jQuery 来满足您的复制和粘贴需求的方法:

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

回答by bleepzter

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.

在 asp.net 中创建对服务的引用。创建一个网页(没有 UI)并在后面的代码中创建多个方法,这些方法是该服务的“包装器”(在 C#/VB.NET 中)。用 [WebMethod] 装饰方法并将 WebMethod 的 Serialization 设置为 JSON。

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

或者,您可以通过为 json Web 服务制作包装器来使用任何其他语言(pearl、php 等)执行相同操作。

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.

您需要该包装器的原因是因为这样您可以避免跨站点脚本... JS 中的限制。此外,如果您的页面通过 HTTPS 提供服务,那么您对包装器的 JS 调用也将通过 HTTPS,因此不必担心安全性。

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

您的 JS 包装器将负责协商连接、身份验证等...

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

您其他页面中的 javascript 可以发布到此页面中的方法:

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

});

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

或 $.post, $.get, $.ajax... 都可以。