【问题标题】:How to pass username and password for a webserivce using jquery ajax call?如何使用 jquery ajax 调用为 Web 服务传递用户名和密码?
【发布时间】:2013-01-27 07:09:00
【问题描述】:

我有一个网络服务;为了访问它,我需要使用凭据。我想在另一个项目中使用 jQuery AJAX 调用 来调用 Web 服务。那么如何使用该方法为该 Web 服务传递用户名和密码呢?

【问题讨论】:

    标签: jquery ajax web-services


    【解决方案1】:

    这是$.ajax 的文档。这也有许多额外的参数。当您需要任何额外的东西时,请按照文档。

    $.ajax({
         type: 'POST',
         url:   'https://webservice url',
         data: ({ username: value, password: value; }) //use parameters as such defined in webservice
         success: function(data){
    
           }
    })
    

    【讨论】:

    • 并确保使用 HTTPS,正如 Chris Stringfellow 在他的评论中已经推荐的那样。没有它,您的密码将不安全。
    • @jeffszusz 根据jquery doc,他们说The ajax() method is used to perform an AJAX (asynchronous HTTP) request. 如何使它成为https
    • 只需在 url 中使用 https:// 而不是 http:// 并确保您的 Web 服务支持 SSL
    【解决方案2】:
    $.ajax({
        type: 'GET',
        url: 'url',
        dataType: 'json',
        //whatever you need
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', make_base_auth(user, password));
        },
        success: function () {});
    });
    
    function make_base_auth(user, password) {
        var tok = user + ':' + password;
        var hash = btoa(tok);
        return 'Basic ' + hash;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多