【问题标题】:ajax call to blogger API对博客 API 的 ajax 调用
【发布时间】:2013-11-09 19:22:41
【问题描述】:

我正在尝试对 blogger.com 进行 ajax 调用。该调用检索用户的数据。我使用这个https://developers.google.com/blogger/docs/3.0/ 作为参考。以下是我写的ajax。

var getUserDetail = function(){

var url = "https://www.googleapis.com/blogger/v3/users/self";

$.ajax({
    url: url,
type: 'GET',
beforeSend: function (request)
    {

        request.setRequestHeader("Authorization","oauth_token_I_got");
     },
}).done(function(data) {
console.log(data)
},"json");

}

每当我尝试拨打电话时都会收到 401 错误

错误的进一步追踪

{
    "error": {
    "errors": [
         {
             "domain": "global",
             "reason": "required",
             "message": "Login Required",
             "locationType": "header",
             "location": "Authorization"
         }
    ],
    "code": 401,
    "message": "Login Required"
    }
}

我哪里错了?谢谢

【问题讨论】:

    标签: jquery ajax google-app-engine google-api blogger


    【解决方案1】:

    由于您从不同的域请求数据,jQuery 很可能会使用 JSONP,并且您不能为 JSONP 请求设置标头(因为它们通过脚本标签工作)。

    我不知道那个特定的 API,但我认为必须有一种 JSONP 兼容的方式来传递您的身份验证令牌(cookie、URL 参数、查询参数或类似的东西)。

    相关问答:Is it Possible to Make Cross-Domain Requests in Javascript AND Set Custom Headers?

    您也可以使用CORS 并以这种方式设置标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2020-07-07
      • 2019-10-11
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      相关资源
      最近更新 更多