【发布时间】:2021-09-23 09:16:01
【问题描述】:
我现在真的迷路了。
我的网页正在加载时出现此错误。
Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.r.onreadystatechange (app.js:115)
这是崩溃的代码:
xmlhttp2.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var dataURL = JSON.parse(this.responseText);
Console.log(this.responsetext) 给出:
[[{"Fecha":"2021-08-16T00:00:00.000Z","pm1":5,"pm2":8,"pm11":1}]]
问题是在我的本地系统上根本没有问题,它工作得很好。而且我不知道从哪里开始,要说当发布时,所有的js类都放在一个类中,所以那里可能有问题。同上。我一直在寻找这个错误,但即使有很多这样的错误,我也无法让它工作
URL 响应格式如下:
[
[
{
"fecha": "2021-09-09T11:40:36.157Z",
"pm1": 5,
"pm2": 8,
"pm10": 11
},
{
"fecha": "2021-09-09T11:39:20.157Z",
"pm1": 5,
"pm2": 6,
"pm10": 6
},
{
"fecha": "2021-09-09T11:38:29.873Z",
"pm1": 11,
"pm2": 14,
"pm10": 15
},
.
.
.
这是上下文的其余代码。也许有用。
window.onload = function () {
var xmlhttp2 = new XMLHttpRequest();
var url2 = "http://My.IP.Adress.real:port/api/grafico/datos/particulas/MedicionAire/2059E7";
xmlhttp2.open("GET", url2, true);
xmlhttp2.send();
xmlhttp2.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var dataURL = JSON.parse(this.responseText);
var pm10 = dataURL.map(function (elemF) {
return elemF.map(function (o) { return { Fecha: o.Fecha, ValorMedicionPM10: o.ValorMedicionPM10 } })
});
var pm1 = dataURL.map(function (elemF) {
return elemF.map(function (o) { return { Fecha: o.Fecha, ValorMedicionPM1: o.ValorMedicionPM1 } })
});
var pm25 = dataURL.map(function (elemF) {
return elemF.map(function (o) { return { Fecha: o.Fecha, ValorMedicionPM25: o.ValorMedicionPM25 } })
});
const dataPointsPM2 = pm25[0].map(function(point) {
return {
label: point.Fecha,
y: point.ValorMedicionPM25,
}
})
const dataPointsPM1 = pm1[0].map(function (point) {
return {
label: point.Fecha,
y: point.ValorMedicionPM1,
}
})
const dataPointsPM10 = pm10[0].map(function (point) {
return {
label: point.Fecha,
y: point.ValorMedicionPM10,
}
})
var chart = new CanvasJS.Chart("chartPM", {
title: {
text: ""
},
zoomEnabled: true,
exportEnabled: true,
data: [
{
type: "line",
showInLegend: true,
legendText: "PM10",
dataPoints: dataPointsPM10
}, {
type: "line",
showInLegend: true,
legendText: "PM1",
dataPoints: dataPointsPM1
}, {
type: "line",
showInLegend: true,
legendText: "PM2.5",
dataPoints: dataPointsPM2
}
]
});
chart.render();
【问题讨论】:
-
您是否尝试登录
this.responseText?该错误声称 JSON 不完整,因此可能缺少部分内容。 -
@Ivar 这就是 this.responseText 给出的: [[{"Fecha":"2021-08-16T00:00:00.000Z","pm1":5,"pm2":8, "pm11":1}]]
-
@Ivar 做一些测试我发现如果那部分代码没有被使用,它就会崩溃......我不明白。
-
这听起来不太合乎逻辑。如果它在不使用时崩溃,那么错误应该指向抛出错误 的其他地方。如果它仍然在该行抛出,则堆栈跟踪的行号未对齐,或者调试器无法正常工作。在您刚刚提供的 JSON 上使用
JSON.parse()shouldn't throw any errors。
标签: javascript url xmlhttprequest