I am trying to display data in JSON format on the browser! any suggestions .... i have given my javascript (.js) code below
我试图在浏览器上以JSON格式显示数据!任何建议....我已经给出了我的javascript(.js)代码
- I am trying to execute this simple query using nodeJS but it dosent seem to work There is no password for the database
- ALL i am trying to do is display the answer in JSON format on the local brower fetched from the mysql database which is also on the local system
- I have a database named node and table named test already created in local system it has just two fields named id and content
我试图使用nodeJS执行这个简单的查询但它似乎工作没有数据库的密码
所有我想要做的是在从本地系统上的mysql数据库获取的本地浏览器上以JSON格式显示答案
我有一个名为node的数据库和名为test的表已在本地系统中创建,它只有两个名为id和content的字段
var http = require('http'),
mysql = require("mysql");
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'node'
});
http.createServer(function (request, response)
{
request.on('end', function ()
{
connection.query('SELECT id, content FROM test WHERE id IN (?, ?)',[1, 2], function(error, rows, fields)
{
response.writeHead(200);
response.end(JSON.stringify(rows));
});
});
}).listen(8080);
my problem is :: nodejs server is running but i am not able to display JSON data on the browser ... any suggestions on how to resolve it !
我的问题是:: nodejs服务器正在运行,但我无法在浏览器上显示JSON数据...有关如何解决它的任何建议!
2 个解决方案
#1
11
This is a 100% working code for your question ! !
这是您的问题的100%工作代码! !
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'node'
});
console.log('MySQL Connection details '+connection);
http.createServer(function (request, response)
{
console.log('Creating the http server');
connection.query('SELECT id, content FROM test WHERE id IN (?,?)',[1, 2], function(err, rows, fields)
{
console.log('Connection result error '+err);
console.log('no of records is '+rows.length);
response.writeHead(200, { 'Content-Type': 'application/json'});
response.end(JSON.stringify(rows));
response.end();
});
}).listen(8084);
I have even added the snapshot's below
我甚至在下面添加了快照
command prompt where the execution is done below
命令提示符,执行在下面完成
JSON output seen in the browser below
在下面的浏览器中看到JSON输出
#2
0
Add a content-type in your code:
在代码中添加内容类型:
response.setHeader("Content-Type", "application/json");
#1
11
This is a 100% working code for your question ! !
这是您的问题的100%工作代码! !
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'node'
});
console.log('MySQL Connection details '+connection);
http.createServer(function (request, response)
{
console.log('Creating the http server');
connection.query('SELECT id, content FROM test WHERE id IN (?,?)',[1, 2], function(err, rows, fields)
{
console.log('Connection result error '+err);
console.log('no of records is '+rows.length);
response.writeHead(200, { 'Content-Type': 'application/json'});
response.end(JSON.stringify(rows));
response.end();
});
}).listen(8084);
I have even added the snapshot's below
我甚至在下面添加了快照
command prompt where the execution is done below
命令提示符,执行在下面完成
JSON output seen in the browser below
在下面的浏览器中看到JSON输出
#2
0
Add a content-type in your code:
在代码中添加内容类型:
response.setHeader("Content-Type", "application/json");