Node.js Express | 尝试获取JSON时始终为空

我对node.js非常陌生,我想从PostgreSQL数据库的HTML5网站上接收JSON。 因此,在服务器端,我使用node-postgres模块进行数据库连接,并使用express模块​​进行通信。 PostgreSQL查询返回一个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.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> 

如果我导航到浏览器上的http://localhost:3000/data ,我会得到:

 {\"type\":\"Point\",\"coordinates\":[-2.994783,43.389217]} 

所以我看到服务器正在发送string化的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> 

客户端现在在http://localhost:8888/prueba/prueba.html

我得到一个JS与以下的回应:

“{\” types\ “:\” 点\ “\ ”坐标\“:[ – 2.994783,43.389217]}”

响应可以在以下屏幕截图中看到:

https://www.dropbox.com/s/zi4c5pqnbctf548/pantallazo.png

但现在警报甚至没有显示

我想我需要一点点这个。

数据已经是一个对象,当你得到它,而不是一个string。 所以JSON.parse失败,因为你期待一个string时,你给它一个对象。 要validation,更改

 alert(JSON.parse(data)); 

 alert(data.type); 

你应该得到“点”

你已经有一个对象的原因是你提供给$.get"json"参数。 如果将其更改为"html" ,则会返回一个string,然后可以将其parsing为JSON对象。

我认为你不应该尝试将你的结果串入响应对象中。 只要把它完全它会自动这样: res.set("Content-Type", 'application/json'); res.json(result.rows[0].json); res.set("Content-Type", 'application/json'); res.json(result.rows[0].json);

这是通过REST API发送信息的正确方法。

然后在你的客户端代码,我不知道它是如何工作,但它应该接受本地JSON。