是否可以在JavaScript中创建跨域请求并设置自定义标题? [英] Is it Possible to Make Cross-Domain Requests in Javascript AND Set Custom Headers?

查看:184
本文介绍了是否可以在JavaScript中创建跨域请求并设置自定义标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于您无法对JSONP调用应用自定义标头 ,我如何使用jQuery进行跨域请求并应用自定义标头?

Since you can't apply custom headers on JSONP calls, how do I make cross domain requests AND apply custom headers using jQuery?

我基本上试图使用jQuery访问google docs,并需要传递一个身份验证令牌: / p>

I'm basically trying to access google docs with jQuery and need to pass an authentication token:

var token = "my-auth-token";
$.ajax({
  url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json",
  dataType: 'json',
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
  },
  success: function(data, textStatus, XMLHttpRequest) {
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
  }
});

注意:这样做的目的是完全绕过应用程序层。使用ruby连接到Google Data API很简单,但是它需要大量的资源在服务器端解析Feed。

Note: The goal of this is to completely bypass the application layer. It's simple to use ruby to connect to the Google Data API, but it takes up a lot of resources parsing feeds all the time server-side.

推荐答案

您可以使用Google的JavaScript客户端库来查询Google文档API。虽然它不是专门用于文档的帮助程序,但它仍然可以用于大多数API,包括Google文档。请参阅此博文显示工作示例的Google员工。

You can use Google's JavaScript client library to query the Docs API. Although it doesn't come with helpers for Docs specifically, it can still be used with most APIs, including Docs. See this blog post by a Google employee that shows a working example.

如果您最终进入无限循环的授权,请参阅相关问题。基本上,Cookie没有设置得足够快,因此当JavaScript客户端库检查时,它找不到任何内容,并重定向到OAuth授权页面。解决方案是在检查完成之前添加一小段延迟,或使用启动授权的登录按钮,而不是在页面加载时执行。

If you end up in an infinite loop of authorizations, see this related question from Google groups. Basically, the cookies aren't getting set fast enough, so when the JavaScript client library checks, it finds nothing and redirects to the OAuth authorization page. A solution is to either add a small delay before the check is done, or use a login button that initiates the authorization instead of doing it on page load.

您还需要向位于同一网域的网页添加任何图片。它可以用CSS隐藏,只要在DOM中。

You would also need to add any image to your page that resides on the same domain. It can be hidden with CSS, as long as in the DOM.

使用上面的博客中的例子,我能够用JavaScript单独检索我的文档列表。这是修改的初始化函数我用来摆脱无限授权循环:

Using the example in the above blog post, I was able to retrieve my documents list with JavaScript alone. Here's the modified initialize function I used to get rid of the infinite authorization loop:

function initialize() {
    var scope = 'http://docs.google.com/feeds/';

    if (google.accounts.user.checkLogin(scope)) {
        var service = new google.gdata.client.GoogleService('writely', 'DocList-App-v1.0');   
        service.getFeed(scope + 'documents/private/full/', handleFeed, handleError);  
    } else {
        var loginButton = $("<button>Click here to login</button>");
        loginButton.click(function() {
            var token = google.accounts.user.login(scope); // can ignore returned token  
        });
        $("body").append(loginButton);
    }
};  
​

这篇关于是否可以在JavaScript中创建跨域请求并设置自定义标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆