Javascript Node.js Express |在尝试获取JSON时总是获取null
我对node.js非常陌生,我想在HTML5网站上接收来自PostgreSQL数据库的JSON。因此,在服务器端,我使用node-postgres模块进行数据库连接,并使用express模块进行通信。PostgreSQL查询正在返回一个JSON对象 服务器端:Javascript Node.js Express |在尝试获取JSON时总是获取null,javascript,json,node.js,postgresql,Javascript,Json,Node.js,Postgresql,我对node.js非常陌生,我想在HTML5网站上接收来自PostgreSQL数据库的JSON。因此,在服务器端,我使用node-postgres模块进行数据库连接,并使用express模块进行通信。PostgreSQL查询正在返回一个JSON对象 服务器端: var express = require('express'); var app = express(); app.get('/data', function(req, res){ var pg = require('pg');
var express = require('express');
var app = express();
app.get('/data', function(req, res){
var pg = require('pg');
var conString = "postgres://postgres:postgres2@localhost/spots";
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
res.send('could not connect to postgres');
}
client.query('SELECT * from spots_json where id=3276', function(err, result) {
if(err) {
res.send('error running query');
}
res.send(JSON.stringify(result.rows[0].json));
client.end();
});
});
});
app.listen(3000);
客户端:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js" ></script>
<script>
$.get('http://localhost:3000/data',{}, function(data){
alert(JSON.parse(data));
},"json");
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
因此,我看到服务器正确地发送了字符串化的JSON,但在客户端,我总是得到空数据。我一定有一些误解。当你得到数据时,它已经是一个对象了,而不是一个字符串。因此,
JSON.parse
失败,因为您在它需要字符串时给了它一个对象。要验证,请更改
alert(JSON.parse(data));
到
你应该得到“要点”
您已经拥有对象的原因是您为
$.get
提供了“json”
参数。如果您将其更改为“html”
,您将得到一个字符串,然后可以将其解析为JSON对象。好的,这就是我的代码到目前为止的情况,对于任何能够提供帮助的人:
服务器端
var express = require('express');
var app = express();
app.get('/data', function(req, res){
var pg = require('pg');
var conString = "postgres://postgres:postgres2@localhost/spots";
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
res.send('could not connect to postgres');
}
client.query('SELECT * from spots_json where id=3276', function(err, result) {
if(err) {
res.send('error running query');
}
res.set("Content-Type", 'text/javascript'); // i added this to avoid the "Resource interpreted as Script but transferred with MIME type text/html" message
res.send(JSON.stringify(result.rows[0].json));
client.end();
});
});
});
app.listen(3000);
客户端
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"></meta>
<meta charset="utf-8"></meta>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js" ></script>
<script>
$.get('http://localhost:3000/data?callback=?',{}, function(data){
alert(data.type);
},"json");
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
试验
$.get('http://localhost:3000/data?callback=?“,{},函数(数据){
警报(数据类型);
}“json”);
客户端现在在http://localhost:8888/prueba/prueba.html
我收到了一个js,回复如下:
“{\'类型\':\'点\',\'坐标\':[-2.994783,43.389217]}”
在以下屏幕截图中可以看到响应:
但现在警报甚至没有显示
我想我需要一些说明。我想当你把结果放在响应对象中时,你不应该试图把它串起来。 就这样说吧,它会自动地:
res.set(“内容类型”,“应用程序/json”);
res.json(result.rows[0].json);
这是通过RESTAPI发送信息的正确方法
然后在你的客户端代码中,我不知道它是如何工作的,但它应该以本机方式接受json。听起来你好像在对它进行双重编码。客户机中
数据的值是什么?我以为数据就是回答的数据。我不太理解代码,因为我几周前就开始编程了。应该是怎样的呢?看我的答案。“数据”已经被$解析。获取
,因此它已经是一个JSON对象,无需再次尝试解析,只需开始使用iThanks进行解释。我理解。但我尝试了这两种方法,但仍然得到空数据…我在Chrome的javascript控制台中得到以下错误:XMLHttpRequest无法加载。访问控制允许原点不允许原点为null。prueba.html:1未捕获类型错误:无法读取nullhmm的属性“0”。这可能是因为它在某种程度上违反了相同的域策略。html文件和node.js文件都在localhost上吗?是的,都在。我读到,也许向url添加回调可以工作$.get('?',{},函数(data){alert(data.type);},“json”);但现在我得到了:资源被解释为脚本,但使用MIME类型text/html:“”传输。我不知道我是否会继续…我不知道如何帮助你。我想你应该不接受我的答案,看看其他人是否能给你一个更好的答案,或者你应该发布另一个问题
var express = require('express');
var app = express();
app.get('/data', function(req, res){
var pg = require('pg');
var conString = "postgres://postgres:postgres2@localhost/spots";
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
res.send('could not connect to postgres');
}
client.query('SELECT * from spots_json where id=3276', function(err, result) {
if(err) {
res.send('error running query');
}
res.set("Content-Type", 'text/javascript'); // i added this to avoid the "Resource interpreted as Script but transferred with MIME type text/html" message
res.send(JSON.stringify(result.rows[0].json));
client.end();
});
});
});
app.listen(3000);
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"></meta>
<meta charset="utf-8"></meta>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js" ></script>
<script>
$.get('http://localhost:3000/data?callback=?',{}, function(data){
alert(data.type);
},"json");
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>