jQuery 修改 JSONP 请求的 HTTP 标头

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

Modify HTTP Headers for a JSONP request

jqueryhttp-headersjsonpuser-agent

提问by Dan D.

I am using jquery to build a request to the Twitter Search API. I am using jsonp, as is needed for cross-domain requests. However, the Twitter API specifies that you should set a unique User-Agent for these requests, and limits your requests if you do not. The problem is, I see no way of setting this header via jquery.

我正在使用 jquery 构建对 Twitter 搜索 API 的请求。我正在使用 jsonp,这是跨域请求所需要的。但是,Twitter API 指定您应该为这些请求设置唯一的用户代理,如果不这样做,则限制您的请求。问题是,我看不到通过 jquery 设置此标头的方法。

This is the code I am using:

这是我正在使用的代码:

$.ajax({
    url: 'http://search.twitter.com/search.json',
    dataType: 'jsonp',
    type: 'get',
    data: { q: 'twitter' },
    success: function(data) {
        alert(data.results);
    }
});

I have tried using the beforeSend method, but it appears that this event is not firing. Can anyone come up with any way of solving this problem?

我曾尝试使用 beforeSend 方法,但似乎没有触发此事件。任何人都可以想出任何方法来解决这个问题吗?

Thanks.

谢谢。

回答by Andy E

JSON with Padding works by adding a script element to the page, with the srcattribute pointing to the URL of the web service. The web service then returns a script containing the data wrapped in a callback function that is executed when the script finishes parsing. It's not so much JSON (it doesn't even have to be valid JSON, for a start) as it is Plain JavaScript.

带有填充的 JSON 通过向页面添加脚本元素来工作,其中src属性指向 Web 服务的 URL。然后,Web 服务返回一个脚本,其中包含包装在回调函数中的数据,该函数在脚本完成解析时执行。它不是 JSON(它甚至不必是有效的 JSON,首先),因为它是纯 JavaScript。

There's no way to modify the headers sent for a script element that's added to your page, unfortunately. The only thing you can do is check for a cross-origin compatible method of retrieving data, such as:

不幸的是,无法修改为添加到页面的脚本元素发送的标头。您唯一能做的就是检查检索数据的跨域兼容方法,例如:

  • XMLHttpRequest Level 2- Chrome, Safari 4+, Firefox 3.5+, Opera

    // Is XMLHttpRequest Level 2 supported?
    if ("withCredentials" in new XMLHttpRequest()) 
  • XDomainRequest- for IE 8, IE 9

    // Is XDomainRequest supported?
    if ("XDomainRequest" in window)
  • XMLHttpRequest 级别 2- Chrome、Safari 4+、Firefox 3.5+、Opera

    // Is XMLHttpRequest Level 2 supported?
    if ("withCredentials" in new XMLHttpRequest()) 
  • XDomainRequest- 适用于 IE 8、IE 9

    // Is XDomainRequest supported?
    if ("XDomainRequest" in window)

It would be a good idea to test for these implementations if they exist and use them accordingly, falling back to standard JSONP for unsupported or older browsers.

如果这些实现存在并相应地使用它们,则测试这些实现将是一个好主意,对于不受支持或较旧的浏览器回退到标准 JSONP。

It's also possible (but unlikely, given that it's high profile) that the web service isn't set up to allow cross-origin requests, so you may still have to fall back to JSONP if the request fails. See also, Cross-Origin Resource Sharing.

也有可能(但不太可能,因为它很引人注目)Web 服务没有设置为允许跨源请求,因此如果请求失败,您可能仍然需要回退到 JSONP。另请参阅跨源资源共享

回答by Azr

Try this :

尝试这个 :

// OAuth configurations   
    var config = {
      'client_id': 'xxxxxx.apps.googleusercontent.com',
      'scope': 'https://www.google.com/m8/feeds/contacts/default/full'          
    };

gapi.auth.authorize(config, function(data) {
      // login complete - now get token
      var token = gapi.auth.getToken();
      token.alt = 'json';
      // retrieve contacts
      jQuery.ajax({
        url: 'https://www.google.com/m8/feeds/contacts/default/full/?max-results=999999',
        dataType: 'jsonp',
        data: token,
        success: function(data) { successGmail(data); }
      });
    });

I found it there : https://groups.google.com/d/msg/google-api-javascript-client/GuFxPzqQ9-0/hZpo041UaH4J

我在那里找到了:https: //groups.google.com/d/msg/google-api-javascript-client/GuFxPzqQ9-0/hZpo041UaH4J