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

时间:2013-11-06 20:18:28

标签: javascript json node.js postgresql

我是node.js的新手,我想在PostgreSQL数据库的HTML5网站上收到JSON。因此,在服务器端,我使用node-postgres模块进行数据库连接,并表示用于通信的模块。 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]}

所以我看到服务器正在发送字符串化的JSON,但在客户端我总是得到空数据。我必须有一些误解。

3 个答案:

答案 0 :(得分:2)

好的,这是我的代码到目前为止,对任何可以帮助的人来说:

服务器侧

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:

“{\” 类型\ “:\” 点\ “\ ”坐标\“:[ - 2.994783,43.389217]}”

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

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

但现在警报甚至没有显示......

我想我需要一些亮点。

答案 1 :(得分:1)

当你得到数据时,数据已经是一个对象,而不是一个字符串。所以JSON.parse失败了,因为你在期待一个字符串时给它一个对象。要验证,请更改

alert(JSON.parse(data));

alert(data.type);

你应该得到“Point”

您已拥有对象的原因是您提供给"json"的{​​{1}}参数。如果将其更改为$.get,则会返回一个字符串,然后您可以将其解析为JSON对象。

答案 2 :(得分:0)

我认为在将结果放入响应对象时,不应尝试对结果进行字符串化。 完全放完它会自动地那样: res.set("Content-Type", 'application/json'); res.json(result.rows[0].json);

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

然后在您的客户端代码中,我不知道它是如何工作的,但它本应接受json。