别在意我上面写的。这只解决了部分时间。似乎 Google Sheets API 根本不支持 CORS。我编写了一个服务器端代理,它只将请求传递到 google.com,这是唯一的前进方向。
我想我会分享我的 js 代码,因为我写了一个很好的小东西,可以像 $.ajax 一样使用。也很高兴分享服务器端代码,但您可以使用类似的东西与您自己的服务器端代理进行交互。它不漂亮,但它正在工作。哦,嗯,LGPL。这是js:
// #### #### ##### #### ## ## ## ## ## ##
// ## ## ## ## ## ## ## #### ## ## #### ## ##
// ## ## ## ##### #### ## ## ## # ## ## ## ####
// ## ## ## ## ## ## ## ###### ####### ###### ##
// #### #### ## ## #### ## ## ## ## ## ## ##
function CorsAway(serverSideUrl) {
// Server-side proxy handling of cross-domain AJAX requests.
this.serverSideUrl = serverSideUrl;
// This hash contains information as to whether each $.ajax parameter should be submitted to $.ajax directly, or passed to the CorsAway server.
// true means that the parameter should be passed to the CorsAway server
this.parameterIsForRemoteServer = {
// accepts: // not supported
// async: // not supported
beforeSend: false, // submit to $.ajax
// cache: // not supported, see $.ajax documentation for how to implement
complete: false, // submit to $.ajax
contents: false, // submit to $.ajax
contentType: true, // submit to remote server
context: false, // submit to $.ajax
converters: false, // submit to $.ajax
// crossDomain: // not supported
data: true, // submit to remote server
dataFilter: false, // submit to $.ajax
dataType: false, // submit to $.ajax
error: false, // submit to $.ajax
// global: // not supported
headers: true, // submit to remote server
// ifModified: // not supported
// isLocal: // not supported
// jsonp: // not supported
// jsonpCallback: // not supported
method: true, // submit to remote server
/// mimeType: true, // submit to remote server
/// password: true, // submit to remote server
// processData: // REQUIRES SPECIAL HANDLING: SEE COMMENTS IN CODE BELOW
// scriptCharset: // not supported
statusCode: false, // submit to $.ajax
success: false, // submit to $.ajax
timeout: false, // submit to $.ajax
// traditional: // not supported
// type: // not supported
/// url: true, // submit to remote server
/// username: true // submit to remote server
// xhr: // not supported
// xhrFields: // not supported
}
// Use it just like $.ajax
this.ajax = function (url, jqAjaxInfo) {
//Redirect all requests to a call to the server
// Sort jqAjaxInfo into parameters for $.ajax and for the remote server
var localAjaxParams = {};
var remoteHttpRequestParams = {};
for(var k in jqAjaxInfo) {
if(this.parameterIsForRemoteServer[k]) {
// Submit it to the remote server
remoteHttpRequestParams[k] = jqAjaxInfo[k];
} else { // some parameters are not supported; their behavior is undefined and doesn't matter
// Submit it to $.ajax
localAjaxParams[k] = jqAjaxInfo[k];
}
}
// Prepare specially encapsulated data parameter for local $.ajax to submit to server-side CorsAway
localAjaxParams.data = {
dataToSubmit: localAjaxParams.data,
remoteHttpRequestParams: remoteHttpRequestParams,
remoteUrl: url
};
localAjaxParams.method = 'PUT'; // Always make request to CorsAway by PUT
// Make call to $.ajax and pass info to server-side CorsAway service
$.ajax(this.serverSideUrl, localAjaxParams);
}
}
// Instantiate global object with URL of server-side CorsAway service
window.corsAway = new CorsAway('/local/url/of/corsaway.php');
所以现在我使用window.corsAway.ajax 而不是$.ajax,结果完全相同。服务器端代理旨在从远程服务器返回数据,或者将它收到的任何 HTML 错误传递回 ajax。
编写一个名为 CorsAway 的实用程序似乎有些错误,但是,嘿。服务器端代理检查域并仅将内容传递到已批准的域(现在只有 Google),所以会出现什么问题,对吧?有人告诉我是否会出错。 :-)