jquery - Is it Possible to Make Cross-Domain Requests in Javascript AND Set Custom Headers? -


How do I make cross domain requests and apply custom headers using jQuery?

I basically need to pass an authentication token by trying to access Google Docs with JQuery and:

  var token = "my-at- Token "; $ .jax ({url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json", datatype: 'jason', first case: function (xhr) { Xhr.setRequestHeader ("authorization", "GoogleLogin auth =" + token);}, success: function (data, text position, XMLHttpRequest) {}, error: function (XMLHttpRequest, textStatus, errorThrown) {}});  

Note: Its goal is to bypass the fully application layer. It's easy to use Ruby to connect to the Google Data API, but there are lots of resources in server-side feeding at all times.

You can use Google's Javascript client library to ask the Docs API. Although it does not come particularly helpful for documents, it can still be used with most APIs including docs. See a Google employee showing an example of doing a job.

If you end up in the infinite loop of the authorization, then see it from Google Groups In fact, cookies are not being set up fast, so when the Javascript client library is checked, it does not seem to be anything and redirects to the Oath authorization page. One solution is to add a short delay before the investigation, or use a login button which starts the authorization instead of doing it on the page load.

You will need to add any picture to your page on the same page. It can be hidden with CSS, as long as it is in DOM.

By using the example in the above blog post, I was able to recover my document list with javascript alone, here is the modified initial function which I was getting 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 + 'document / private / full /', handlid, handler); } Else {var loginButton = $ ("& lt; Button & gt; Click here to login & lt; / button & gt;"); LoginButton.click (function () {var token = google.accounts.user.login (scope); // can ignore token back}); $ ("Body") enclosed (loginButton). }};  

Comments