jQuery JSONP ajax,未设置身份验证标头

发布于 2024-12-04 18:43:30 字数 475 浏览 2 评论 0原文

我正在尝试使用以下设置向 google 联系人 API 发出 ajax 请求:

$.ajax({
  url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all",
  dataType: 'jsonp',
  data: {
    alt: 'json-in-script'
  },
  headers: {
    'Authorization': 'Bearer ' + token
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  }
});

但是 Authentication 标头似乎没有设置。有什么想法吗?

请求

I'm trying to make an ajax request to the google contacts API with the following setup:

$.ajax({
  url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all",
  dataType: 'jsonp',
  data: {
    alt: 'json-in-script'
  },
  headers: {
    'Authorization': 'Bearer ' + token
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  }
});

But the Authentication header doesn't seem to get set. Any ideas?

The request

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半窗疏影 2024-12-11 18:43:30

我最近也遇到了同样的问题。试试这个:

$.ajax({
  url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all",
  dataType: 'jsonp',
  data: {
    alt: 'json-in-script'
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  },
  beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + token); } 
});

编辑:看起来不能用 JSONP 来完成。 修改 JSONP 请求的 HTTP 标头

I had the same problem recently. Try this:

$.ajax({
  url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all",
  dataType: 'jsonp',
  data: {
    alt: 'json-in-script'
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  },
  beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + token); } 
});

EDIT: Looks like it can't be done with JSONP. Modify HTTP Headers for a JSONP request

柠檬 2024-12-11 18:43:30

当跨域请求需要身份验证时,您必须使用某种代理服务器。

由于使用 dataType: jsonp 会导致 HTTP 请求实际上是从添加到 DOM 的脚本发出的,因此不会使用 $.ajax 中设置的标头。

When authentication is needed in a cross domain request, you must use a proxy server of some sort.

Since using dataType: jsonp results in the HTTP request actually being made from the script that gets added to the DOM, the headers set in the $.ajax will not be used.

寂寞陪衬 2024-12-11 18:43:30

似乎大多数 OAUTH2 REST 资源都接受 access_token 参数作为请求 url 的一部分

http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param

请尝试以下代码 反而:

$.ajax({
            dataType: 'jsonp',
            url: url,                
            data: {
                'access_token':token.access_token
            },
            jsonpCallback: 'thecallback',
            success: function(data){
                _cb(data);
            },
            error: function(d){
                _cb(d);
            }
        });

Is seems that most of the OAUTH2 REST resources accept the access_token parameter as part of the request url

http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param

please, try the following code instead:

$.ajax({
            dataType: 'jsonp',
            url: url,                
            data: {
                'access_token':token.access_token
            },
            jsonpCallback: 'thecallback',
            success: function(data){
                _cb(data);
            },
            error: function(d){
                _cb(d);
            }
        });
梦冥 2024-12-11 18:43:30

只需这样做(jquery 2.0,但应该可以在以前的版本中使用)

    $.ajax({
        url: "/test",
        headers: {"Authorization": "Bearer " + $('#myToken').val()}
    })           
    .done(function (data) {
      console.log(data);
    })
    .fail(function (jqXHR, textStatus) {
      alert("error: " + textStatus);
    });

Just do this (jquery 2.0, but should work in previous versions)

    $.ajax({
        url: "/test",
        headers: {"Authorization": "Bearer " + $('#myToken').val()}
    })           
    .done(function (data) {
      console.log(data);
    })
    .fail(function (jqXHR, textStatus) {
      alert("error: " + textStatus);
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文