JSON.parse意外令牌位于位置0

时间:2019-10-24 18:52:03

标签: javascript json

我正在尝试学习JSON并观看Udemy视频。在执行代码的过程中,我在位置0处获取了JSON.parse意外令牌。我不太确定如何解决该错误或它所包含的全部内容。

我已经检查了视频的“问答”部分,似乎很多人都遇到了相同的错误,但没有解决方案。我还检查了一些stackoverflow答案,但是我对JSON的概念非常陌生,以至于它没有意义。任何帮助将不胜感激!

Now listening on: https://localhost:5002

Now listening on: http://localhost:5003

Application started. Press Ctrl+C to shut down.

info: Microsoft.AspNetCore.Server.Kestrel[20]

      Connection id "0HLQOL2OADVUQ" request processing ended abnormally.

System.IO.IOException: The decryption operation failed, see inner exception. ---> System.ComponentModel.Win32Exception: Error desconocido al procesar el certificado

HTML部分

const express = require("express");

const bodyParser = require("body-parser");

const request = require("request"); // use to do a request to an external 
server

const app = express();



app.use(bodyParser.urlencoded({extended: true}));



 app.get("/", function(req, res) {

 res.sendFile(__dirname + "/index.html");

 });



app.post("/", function(req, res) {



 var crypto = req.body.crypto;

 var fiat = req.body.fiat;



 var URL = "https://apiv2.bitcoinaverage.com/indices/global/ticker/";

 var finalURL = URL + crypto + fiat;

 request(finalURL, function(error, response, body) {



var data = JSON.parse(body)

var price = data.last;

var currentDate = data.display_timestamp;



res.write("The current date is " + currentDate);

res.write("<h1>The current price of" +  crypto + " is: " + price + fiat + 
"</h1>");

res.send();

});

});

app.listen(3000, function() {

console.log("Server started on port 3000.");



});

预期的输出应该是当您选择加密货币和货币(例如美元,英镑,欧元)时,它将告诉您加密货币的价格。

1 个答案:

答案 0 :(得分:0)

当使用错误的URL对此API发出请求时,您将返回一个字符串,该字符串将导致JSON.parse返回此错误。 API提供程序可能超时更改了此链接。尝试在浏览器中访问此api路由,看看是否可行。

很可能您传递了错误的URL。