我已经看到几个类似问题的问题,但无法解决这个问题。
我得到的错误是:
Uncaught TypeError: Cannot read property 'url' of undefined
at XMLHttpRequest.xhr.onload (large-file.js:103)
第 103 行是这样的:
instance.toResolve(pkt.ctrl.params.url);
代码:
this.xhr.onload = function() {
let pkt;
alert(this.response);
try {
pkt = JSON.parse(this.response, jsonParseHelper);
} catch (err) {
instance._tinode.logger("ERROR: Invalid server response in LargeFileHelper", this.response);
pkt = {
ctrl: {
code: this.status,
text: this.statusText
}
};
}
if (this.status >= 200 && this.status < 300) {
if (instance.toResolve) {
instance.toResolve(pkt.ctrl.params.url);
}
if (instance.onSuccess) {
instance.onSuccess(pkt.ctrl);
}
...
function jsonParseHelper(key, val) {
// Convert string timestamps with optional milliseconds to Date
// 2015-09-02T01:45:43[.123]Z
if (key === 'ts' && typeof val === 'string' &&
val.length >= 20 && val.length <= 24) {
let date = new Date(val);
if (date) {
return date;
}
} else if (key === 'acs' && typeof val === 'object') {
return new AccessMode(val);
}
return val;
};
我出于测试目的加入了 alert(pkt);
,它显示:
JSON.parse 由于某种原因不起作用:
[17:39:41.547] ERROR: Invalid server response in LargeFileHelper {"ctrl":{"id":"69834","params":{"url":"/v0/file/s/zYrwWlHrrGI.bin"},"code":200,"text":"ok","ts":"2021-06-09T17:39:45.719Z"}}
响应有效。这个响应来自 Nginx 代理并且响应头包含以下内容可能是一个问题?
Content-Type: application/json; charset=utf-8
如果我可以(警报)从服务器收到响应,它是一个字符串,但 Nginx 代理会自动添加 Content-Type: application/json;字符集=utf-8