简体   繁体   English

HTTP请求JSON:意外的.parse错误-如何消除它?

[英]HTTP Request JSON: Unexpected .parse error - How do I exterminate it?

I am using IBM Bluemix to make a web service for a school project. 我正在使用IBM Bluemix为学校项目提供Web服务。

My project needs to request a JSON from an API, so I can use the data it provides. 我的项目需要从API请求JSON,因此我可以使用它提供的数据。

I am having trouble with the http request to the API service. 我对API服务的http请求遇到问题。 I get the following alert in the Windows 10 Command Prompt. 我在Windows 10命令提示符中收到以下警报。

"Syntaxerror: Unexpected Token" “语法错误:意外的令牌”

I know there is something wrong with my JSON request, but is it exactly? 我知道JSON请求有问题,但这是真的吗?

Here is my .js file and a print of the error screen I get when running it. 这是我的.js文件和运行它时得到的错误屏幕的打印图像。

 /*eslint-env node*/ //------------------------------------------------------------------------------ // node.js starter application for Bluemix //------------------------------------------------------------------------------ // HTTP request - duas alternativas var http = require('http'); var request = require('request'); // cfenv provides access to your Cloud Foundry environment // for more info, see: https://www.npmjs.com/package/cfenv var cfenv = require('cfenv'); //chama o express, que abre o servidor var express = require('express'); // create a new express server var app = express(); // serve the files out of ./public as our main files app.use(express.static(__dirname + '/public')); // get the app environment from Cloud Foundry var appEnv = cfenv.getAppEnv(); // start server on the specified port and binding host app.listen(appEnv.port, '0.0.0.0', function() { // print a message when the server starts listening console.log("server starting on " + appEnv.url); }); app.get('/home1', function(res){ http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){ var body = ''; res.on('data', function(chunk){ body += chunk; }); res.on('end', function(){ var json = JSON.parse(body); }); var json = JSON.parse(res); var cotacao = json["bovespa"]["cotacao"]; console.log("A sua cotação é "+cotacao); }); }); 

Print of the Error Screen in the Command Line 在命令行中打印错误屏幕

You need to use the json object you created in .on('end' 您需要使用在.on('end'

trying to JSON.parse res when res is clearly not a string is causing your error (res.toString() results in [Object object] ... hence the error as that is not valid JSON res显然不是字符串时尝试JSON.parse res导致您的错误(res.toString()导致[Object object] ...因此错误,因为该错误无效JSON

app.get('/home1', function(res){
    http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){
        var body = '';
        res.on('data', function(chunk){
            body += chunk;
        });
        res.on('end', function(){
            var json = JSON.parse(body);
            var cotacao = json["bovespa"]["cotacao"];

            console.log("A sua cotação é "+cotacao);
        });
    });
});

Try configuring app for parsing json body: 尝试配置用于解析json主体的应用程序:

app.configure(function () {
  app.use(express.json())
})

You will recieve a parsed json object 您将收到一个已解析的json对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在 JSON.parse 错误时收到 Unexpected end of JSON input - Why do I get an Unexpected end of JSON input at JSON.parse error 如何解析来自 GET 请求的 json 响应 - How do I parse a json response from a GET request AJAX请求错误:“SyntaxError:JSON.parse:unexpected character” - AJAX request error: “SyntaxError: JSON.parse: unexpected character” JSON 解析错误:针对获取 API 请求的意外标识符“数组” - JSON Parse error: Unexpected identifier "Array" against Fetch API request 发出AJAX请求时如何解决“ JSON解析错误:意外的标识符数组” - How to fix 'JSON Parse Error: unexpected identifier array' when making an AJAX Request 尝试在简单对象上执行JSON.parse时出现“意外令牌”错误 - I am getting an “Unexpected token” error when trying to do JSON.parse on a simple object 如何在$ scope的get http请求中分离json对象? - How do I separate the json objects in a get http request in a $scope? 我如何解决此错误“语法错误:JSON中位置0上的意外令牌&lt;” - How do i resolve this error “ SyntaxError: Unexpected token < in JSON at position 0” JSON解析错误意外令牌 - JSON parse error unexpected token JSON 解析错误:意外的标识符“be” - JSON Parse error: Unexpected identifier "be"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM