Chrome 以外的浏览器中的 JSON.parse 错误



我在从 Chrome 以外的浏览器访问时解析获取的 JSON 数据时遇到问题。Firefox 返回最详细的错误:"SyntaxError: JSON.parse:JSON 数据第 1 行第 1 列的意外字符"。在某些情况下,当代码托管在我的本地节点.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 中运行良好,但在 Firefox 中无法正常工作,当我传输要由 SharePoint 托管的文件时:

演示(请参阅控制台.log(:https://allenblair.github.io/fetchissue/

源文件:https://github.com/allenblair/fetchissue/

我的代码中是否有需要不同的内容?

这是由 Firefox 中返回 401 未授权的获取引起的 所以是的,谢谢你@JLRishe 我们必须在标题中添加credentials: 'include'

我在SharePoint 2016中将其与反应一起使用

componentDidMount() {

let headers = {
method: 'GET',
credentials: 'include',
headers: {
Accept: "application/json;odata=verbose"
},
mode: 'cors',
cache: 'default'
};
let siteURL = _spPageContextInfo.siteAbsoluteUrl;
let apiPath = siteURL + "/_api/web/lists/getbytitle('linksfooter')/items?$select=Title,LinkFooterColonne,LinkFooterContent,LinkFooterPosCol";

fetch(apiPath, headers)
.then(
res => res.json()
).then(
(d) => {
// console.log('fetched data = ', d)
let footerItemsResult = d.d.results;
let categories = footerItemsResult.map(item => item.LinkFooterColonne).filter(function (v, i, a) {
return a.indexOf(v) === i
})
// console.log('footerItemsResult = ', footerItemsResult)
this.setState({
isLoaded: true,
footerItems: footerItemsResult,
categories: categories
});
},
(error) => {
this.setState({
isLoaded: false,
error
});
}
)
}

相关内容

最新更新