【发布时间】:2020-10-12 17:46:44
【问题描述】:
我在从 Chrome 以外的浏览器访问时解析获取的 JSON 数据时遇到问题。 Firefox 返回最详细的错误:“SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data”。在某些情况下,当托管在我的本地 node.js 开发环境中时,代码将在其他浏览器中运行,但只有在文件由 SharePoint 托管时才在 Chrome 中运行。在我上传到 github 页面的示例中,它适用于 Chrome 而不是 Firefox 等。人。
function loadData() {
return fetch('./data.txt').then((response) => response.json());
}
function loadMoreData() {
return fetch('./moredata.txt').then((response) => response.json());
}
function loadAllData(){
return Promise.all([loadData(),loadMoreData()]);
}
loadAllData().then( ([data, moredata]) => {
console.log(data);
console.log(moredata);
});
数据.txt:
[
{"emp_id":"90176","labor_code":"500"},
{"emp_id":"90202","labor_code":"500"},
{"emp_id":"90678","labor_code":"400"},
{"emp_id":"91245","labor_code":"300"},
{"emp_id":"91304","labor_code":"200"},
{"emp_id":"94377","labor_code":"100"},
{"emp_id":"94398","labor_code":"200"}
]
更多数据.txt:
[
{"emp_id": "90176","hire_year": "1999"},
{"emp_id": "90202","hire_year": "2010"},
{"emp_id": "90678","hire_year": "2005"},
{"emp_id": "91245","hire_year": "1994"},
{"emp_id": "91304","hire_year": "1995"},
{"emp_id": "94377","hire_year": "1995"},
{"emp_id": "94398","hire_year": "1998"}
]
这里是 github 数据的位置,它在 Chrome 和 Firefox 中运行良好,但在我传输要由 SharePoint 托管的文件时在 Firefox 中无法运行:
演示(见console.log):https://allenblair.github.io/fetchissue/
源文件:https://github.com/allenblair/fetchissue/
我的代码中是否有什么需要不同的地方?
【问题讨论】:
-
我在 Firefox 中没有看到任何错误。两个数组记录到控制台。
-
是的,我刚刚意识到我的错误。这种情况在 github 上看起来不错,但是在将相同的文件传输到 SharePoint 时,它会产生错误。不幸的是,我没有公共的共享点服务器来演示错误。
-
所以查看 Firefox 控制台中的文件,看看里面有什么。接下来改为阅读
console.log(escape(response.text())并查看其中的内容。我的猜测是其中可能有一些隐藏的字符...... -
@AllenBlair 这是一个承诺。你需要做
response.text().then(t => console.log(t)) -
@AllenBlair 尝试在
fetch选项中包含credentials: 'include'。
标签: javascript json sharepoint