gpt4 book ai didi

javascript - 在 Backbone.js 中下载 Excel

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:24 26 4
gpt4 key购买 nike

  //Download SKU logic
downloadSku: function (e) {
e.preventDefault();
var checkedValues = this.$el.find('.chk:checked').map(function () {
return this.value;
}).get();
var options = {
success: function (model, response) {
console.log(response);
var blob = new Blob([response], { type: 'application/vnd.ms-excel' });

var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "downloadFile.xlsx";
document.body.appendChild(a);
a.click();
}
};
var obj = {};
obj["sku_ids"] = checkedValues;
this.downloadSkuModel.set(obj, { validate: true });
this.downloadSkuModel.save({}, options);

}

服务.js

    var services = {

sync: function (method, model, options) {
var self = this;
var api_token = mainJs.get_api_token();
var access_token;
if (api_token == null) {
access_token = api_token;

} else {
access_token = api_token.access_token;
}

options || (options = {});

var beforeSend = options.beforeSend;
options.beforeSend = function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token)
if (beforeSend) return beforeSend.apply(this, arguments);
};

switch (method) {
case "read":
options.url = options.readUrl;
break;
case "delete":
options.url = options.deleteUrl;
break;
case "update":
options.url = options.updateUrl;
options.contentType = 'application/json';
break;
case "create":
options.url = options.createUrl;
options.contentType = 'application/json';
break;
}

options.error = function (xhr, statusTxt, thrown) {
switch (xhr.status) {
case 401:
console.log("Unauthorized error");
break;
default:
var messageText = JSON.parse(xhr.responseText);;
console.log("Status code: " + xhr.status + " Error: " + messageText.message);
}

}

if (options.url)
return Backbone.sync.call(model, method, model, options);

}

}


module.exports = services;

我的响应头:

  HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
Content-Disposition: attachment;filename=sku_list.xlsx
Content-Type: application/octet-stream;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 15 Sep 2015 14:56:58 GMT

我从服务器收到响应,但它没有出现在 View 中。

如果我在我看来使用 Javascript,它会起作用:

 var obj = {};
obj["sku_ids"] = checkedValues;
var xhr = new XMLHttpRequest();
xhr.open('POST', Urls.sku.download, true);
xhr.responseType = 'blob';

xhr.setRequestHeader("Authorization", "Bearer " + "somethign");
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.onload = function (e) {
if (this.status == 200) {
var blob = new Blob([this.response], { type: 'application/vnd.ms-excel' });
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "downloadSku.xlsx";
a.setAttribute("data-bypass", "");
document.body.appendChild(a);
a.click();
} else {
alert('Unable to download excel.')
}
};
xhr.send(JSON.stringify(obj));

我遇到的 backbone 错误如下:

SyntaxError: Unexpected token P at Object.parse (native) at K.parseJSON 

最佳答案

我解决了我的问题:

这是我做的

我更改了我的 options.dataType = 'text' 因为默认情况下 backbone 假设它是一个 json 响应并且正在做一个 Object.parse

关于javascript - 在 Backbone.js 中下载 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589563/

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