【发布时间】:2017-07-29 20:37:42
【问题描述】:
我在 Google API 中的 $.ajax 请求中收到 404 错误。 我有这些代码,
var asyncLoad = require('react-async-loader');
var CLIENT_ID = '<SOME_ID>';
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];
var SCOPES = 'https://www.googleapis.com/auth/drive';
const mapScriptToProps = state => ({
gapi: {
globalPath: 'gapi',
url: 'https://apis.google.com/js/api.js'
}
});
@asyncLoad(mapScriptToProps)
...
我在 bundle.js 之前使用了 react 的异步加载器来获取 Google API。 然后我得到了属性中的gapi。这是我提交表单的下一个代码。
submitForm(e){
e.preventDefault();
var data = this.refs.file.files[0];
var self = this;
var formData = new FormData();
formData.append('data', data);
$.ajax({
url: "https://www.googleapis.com/upload/drive/v3?uploadType=media&access_token="+encodeURIComponent(self.state.token),
type: "POST",
processData: false,
data: formData,
beforeSend: function (xhr) {
/* Authorization header */
xhr.setRequestHeader("Authorization", "Bearer " + self.state.token);
xhr.setRequestHeader('X-Upload-Content-Length', data.size);
xhr.setRequestHeader("Content-Type", "image/jpeg");
xhr.setRequestHeader('X-Upload-Content-Type', "image/jpeg");
},
success: function(data){
if(typeof data === "string") data = JSON.parse(data);
console.log(data);
if(data.success){
console.log("done");
}else {
console.log("error");
}
}
});
}
所以在这里,我在点击按钮上传时调用 submitForm 函数。我也有ref="file" 的文件输入。这是在客户端(浏览器)端运行的。我收到 404 错误。
我在这里要做的是将图像文件上传到谷歌驱动器。我怎样才能做到这一点?我的问题有什么解决方案吗?
【问题讨论】:
标签: javascript jquery ajax node.js reactjs