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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7433556/
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-24 02:15:09  来源:igfitidea点击:

jQuery JSONP ajax, authentication header not being set

javascriptjqueryhttp-headersjsonp

提问by David Tuite

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

我正在尝试使用以下设置向谷歌联系人 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);
  }
});

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

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

The request

请求

回答by Andrew Church

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

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

回答by Dangladesh

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

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

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

由于使用dataType: jsonpHTTP 请求中的结果实际上是从添加到 DOM 的脚本中生成的,因此$.ajax不会使用 中设置的标头。

回答by user1413048

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

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

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

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);
            }
        });

回答by rynop

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

就这样做(jquery 2.0,但应该在以前的版本中工作)

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