【问题标题】:HTTP Authentication failing in AJAX for postmates apipostmates api在AJAX中的HTTP身份验证失败
【发布时间】:2016-04-30 21:51:37
【问题描述】:

我正在尝试使用 postmates API,它首先要求我们使用 http 基本身份验证对自己进行身份验证。下面代码中的username 字段是我们插入private API key 的位置。

 <script>
    $(document).ready(function(){
        // Request with custom header
        $.ajax({
            url: ' http://username:@api.postmates.com',
            type: 'GET',
            dataType: 'jsonp',
            success: function(response) { alert("Success"); },
            error: function(error) {alert(error); }
        });
    });
    </script>

我们得到的错误是

XMLHttpRequest 无法加载 http://api.postmates.com/?callback=jQuery112008309037607698633_1462052396724&_=1462052396725。 预检响应无效(重定向)

【问题讨论】:

    标签: javascript ajax postmates


    【解决方案1】:

    需要认证

    https://postmates.com/developer/docs#authentication

    实际使用的标头将是 base64 编码的字符串,例如 这个:

    基本 Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==

    尝试

    $(document).ready(function(){
            // Request with custom header
            $.ajax({
                url: ' http://username:@api.postmates.com',
                type: 'GET',
                dataType: 'jsonp',
                success: function(response) { alert("Success"); },
                error: function(error) {alert(error); },
                beforeSend: function (xhr) {
                        xhr.setRequestHeader ("Authorization", "Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==");
                        }
            });
        });
    

    我不测试,因为 jsfiddle 会阻止外部请愿。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2014-01-24
      相关资源
      最近更新 更多