来自Google Docs API的数据供稿

I am trying to use the Google Docs API to get spreadsheet data as XML, and eventually JSON data. I have put the URL (http://spreadsheets.google.com/feeds/list/0Aizy-VIdLC0QdDNNRkpfVncxQzZRNG9fMVhueXVMenc/1/private/values) in my browser, and I get the data, even when not logged in to Google.

When I try with jQuery Ajax, I get the "page not found" error.

$.get('http://spreadsheets.google.com/feeds/list/0Aizy-VIdLC0QdDNNRkpfVncxQzZRNG9fMVhueXVMenc/1/private/values', function(data) {
  console.log(data)
});

I am guessing that same origin policy might be the cause of the Ajax error, so I tried with PHP, but I get the error.

echo file_get_contents("http://spreadsheets.google.com/feeds/list/0Aizy-VIdLC0QdDNNRkpfVncxQzZRNG9fMVhueXVMenc/1/private/values");

I am aiming to get the Google Docs spreadsheet data into a JSON object for use on a webpage.

How can I fix this error?

Supposing you double-checked your URL string, the reason could be same origin policy. You cannot fetch data from a different domain due to browser's strict security policy.

If you want to get past this, you need to use a different approach, such as JSONP.

You can read more about how to implement JSONP in jQuery on the Ajax documentation page.

You have to be logged in in order to be able to retrieve the spreadsheet data.

Visiting http://spreadsheet.google.com/.../private/values while logged in results in a meaningful XML page. Trying to load the same page when not logged in, however, results in a "Page Not Found".

According to the response header from the JQuery response, the only cause of this error is not being logged in (shown below).

WWW-Authenticate: No credentials were included in your request.

See The Protocol Guide for the relevant documentation.