0

这可能是一个愚蠢的问题,但我是 Web 编程的新手。我正在尝试使用客户端 JavaScript 和 CORS 与 Google Drive 通信。我首先使用了 jsclient 库,效果很好:

request = gapi.client.drive.files.list( {'q': " trashed = false " } );

使用 CORS,我的代码如下所示:

var xhr = new XMLHttpRequest();
xhr.open('GET','https://www.googleapis.com/drive/v2/files');

var mysearch = encodeURIComponent("q=trashed=false");

xhr.open('GET',"https://www.googleapis.com/drive/v2/files?" +mysearch,true);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.onload = function() { handleResponse(xhr.responseText); };
xhr.onerror = function() { handleResponse(null); };
xhr.send();

我努力了:

var mysearch = encodeURIComponent("q=trashed=false");
var mysearch = encodeURIComponent("trashed=false");
var mysearch = encodeURIComponent("q='trashed=false'");

它们都返回所有文件的列表。如果我没有搜索字符串,我也会得到所有文件。

我还想有其他搜索参数,使用 &,但我不能只使用一个。如何格式化 mysearch 字符串?

4

1 回答 1

0

仅对参数的值部分进行编码:

var url = 'https://www.googleapis.com/drive/v2/files?q=' + encodeURIComponent("'trashed=false'")
于 2013-10-21T08:53:43.260 回答