从javascript加载json时出错
Getting error trying to load json from javascript
我的javascript有一些问题。我试图加载json数组ag-grid像这样,下面的工作很好,因为我只是从json文件加载json:
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', '../dist/output.json');
httpRequest.send();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var httpResult = JSON.parse(httpRequest.responseText);
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
var parsedData = httpResult.map(function(obj) {
return Object.keys(obj).reduce(function(memo, key) {
var value = obj[key];
memo[key] = isNumeric(value) ? Number(value) : value;
return memo;
}, {})
})
但是,当我以以下方式执行时(即,从jsp获取json数组),console.log(jsonArray)显示良好,但我得到错误:
var jsonArray = document.getElementById("jsonArray");
console.log(jsonArray);
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', jsonArray);
httpRequest.send();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var httpResult = JSON.parse(httpRequest.responseText);
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
var parsedData = httpResult.map(function(obj) {
return Object.keys(obj).reduce(function(memo, key) {
var value = obj[key];
memo[key] = isNumeric(value) ? Number(value) : value;
return memo;
}, {})
})
我得到以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
如何改变第一个代码,以便我可以正确地从jsonArray中读取,其中包含所有json数据。
更新:jsonArray是jsp中的以下内容:
JSONArray jsonArray = new JSONArray(orderDetailsList1);
<input type="hidden" value="<%out.println(jsonArray);%>" id="jsonArray"/>
jsonArray
是一个DOM对象。要访问它的value
属性,使用value
属性:
httpRequest.open('GET', jsonArray.value);
相关文章:
- 解析JSON并从中删除对象会出错
- 加载db.json时出错
- 解析将数据从 JSON 发送到 Django 时出错
- 使用 RESTful API,如果未定义特定的响应 json 级别,我如何在客户端不出错
- 从静态文件加载json时出错
- 解析从js发送到Java的json字符串时出错
- JSON.parse在循环对象上未出错
- 将JSON映射到observableArray时出错
- 406向Spring Controller发送JSON时出错
- 在NodeJS中编辑JSON数据时出错
- 尝试从JSON加载单词时出错
- 使用JQuery和JSON将对象发送到JAX-RS时出错
- 在变量中存储 JSON 数据时出错
- 从标记内容解析 JSON 时出错
- 从 JavaScript 中的本地 JSON 文件读取时出错
- 转换为 JSON 的 XML 节点会出错
- Newtonsoft Json.net 解析布尔值时出错
- 405 AJAX 调用 JSON 时出错(xhr.status)
- 在 JavaScript 中解析 JSON 数据时出错
- 序列化或反序列化过程中出错.JSON JavaScriptSerializer