gpt4 book ai didi

javascript - 如何将 XMLHttpRequest 响应的一部分存储为变量?

转载 作者:行者123 更新时间:2023-12-03 03:39:32 25 4
gpt4 key购买 nike

我正在使用 this tutorial 的修改版本使用表单将文件上传到 Google 云端硬盘。

相关代码如下:

$("#fUpload").on("change", function () {
var uploadObj = $("[id$=fUpload]");
var file = uploadObj.prop("files")[0];
var metadata = {
'title': file.name,
'description': " ",
'mimeType': file.type || 'application/octet-stream',
"parents": [{
"kind": "drive#file",
"id": "0B5zM5ktmwJ2fN0c3RWYxWC1rUzQ"
}]
};
var arrayBufferView = new Uint8Array(file);
var uploadData = new Blob(arrayBufferView, {type: file.type || 'application/octet-stream'});
try{
var uploader =new MediaUploader({
file: file,
token: gapi.auth.getToken().access_token,
metadata: metadata,
params: {
convert:false,
ocr: false
}
});
uploader.upload();
}catch(exc){
showErrorMessage("Error: " + exc);
$("#fUpload").val(" ");
}
});

var MediaUploader = function (options) {
var noop = function () { };
this.file = options.file;
this.contentType = options.contentType || this.file.type || 'application/octet-stream';
this.metadata = options.metadata || {
'title': this.file.name,
'mimeType': this.contentType
};
this.token = options.token;
this.onComplete = options.onComplete || noop;
this.onProgress = options.onProgress || noop;
this.onError = options.onError || noop;
this.offset = options.offset || 0;
this.chunkSize = options.chunkSize || 0;
this.retryHandler = new RetryHandler();

this.url = options.url;
if (!this.url) {
var params = options.params || {};
params.uploadType = 'resumable';
this.url = this.buildUrl_(options.fileId, params, options.baseUrl);
}
this.httpMethod = options.fileId ? 'PUT' : 'POST';
};

/**
* Initiate the upload.
*/
MediaUploader.prototype.upload = function () {
var self = this;
var xhr = new XMLHttpRequest();

xhr.open(this.httpMethod, this.url, true);
xhr.setRequestHeader('Authorization', 'Bearer ' + this.token);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('X-Upload-Content-Length', this.file.size);
xhr.setRequestHeader('X-Upload-Content-Type', this.contentType);

xhr.onload = function (e) {
if (e.target.status < 400) {
var location = e.target.getResponseHeader('Location');
this.url = location;
this.sendFile_();
} else {
this.onUploadError_(e);
}
} .bind(this);
xhr.onerror = this.onUploadError_.bind(this);
xhr.send(JSON.stringify(this.metadata));
};

/**
* Send the actual file content.
*
* @private
*/
MediaUploader.prototype.sendFile_ = function () {
var content = this.file;
var end = this.file.size;

if (this.offset || this.chunkSize) {
// Only bother to slice the file if we're either resuming or uploading in chunks
if (this.chunkSize) {
end = Math.min(this.offset + this.chunkSize, this.file.size);
}
content = content.slice(this.offset, end);
}

var xhr = new XMLHttpRequest();
xhr.open('PUT', this.url, true);
xhr.setRequestHeader('Content-Type', this.contentType);
xhr.setRequestHeader('Content-Range', "bytes " + this.offset + "-" + (end - 1) + "/" + this.file.size);
xhr.setRequestHeader('X-Upload-Content-Type', this.file.type);
if (xhr.upload) {
xhr.upload.addEventListener('progress', this.onProgress);
}
xhr.onload = function(event){
var xhr = event.target;

if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}


xhr.onerror = this.onContentUploadError_.bind(this);
xhr.send(content);
};

我需要将响应中指定的 ID 存储为变量。响应正文looks like this 。问题是我不知道如何到达它。我认为成功处理程序应该处理这个问题,但事实并非如此:

/**
* Handle successful responses for uploads. Depending on the context,
* may continue with uploading the next chunk of the file or, if complete,
* invokes the caller's callback.
*
* @private
* @param {object} e XHR event
*/
MediaUploader.prototype.onContentUploadSuccess_ = function (e) {
if (e.target.status == 200 || e.target.status == 201) {
this.onComplete(e.target.response);

} else if (e.target.status == 308) {
this.extractRange_(e.target);
this.retryHandler.reset();
this.sendFile_();
}
};

我尝试过 console.log(e.target.response)console.log(e.target.responseText)

var response = e.target.response;
console.log(response);

似乎没有什么效果。我什至无法让控制台输出字符串。

抱歉,如果有什么愚蠢的事情,我还是很菜鸟。

最佳答案

从您的问题评论来看,您似乎已经得到了想要的答复。只需在范围之外声明一个变量并将响应的 ID 属性分配给它即可。

var myId;
MediaUploader.prototype.onContentUploadSuccess_ = function (e) {
if (e.target.status == 200 || e.target.status == 201) {
console.log(e.target.response); // assuming this is the result of "http://imgur.com/a/KgDQO"
myId = e.target.response.id; // bam
this.onComplete(e.target.response);

} else if (e.target.status == 308) {
this.extractRange_(e.target);
this.retryHandler.reset();
this.sendFile_();
}
};
console.log(myId);

关于javascript - 如何将 XMLHttpRequest 响应的一部分存储为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45699616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com