2016-03-19 9 views
0

Es tut mir leid, wenn dies eine wiederholte Frage ist. aber, ich versuche, eine Möglichkeit zu finden, die Daten von meinem Nodejs in HTML anzuzeigen.Anzeige der Daten von Nodejs in HTML

Es folgt mein data.js Code:

var express = require('express'); 
var app = express(); 
var path = require('path'); 

var data = new Array;  
data.push([1,2,3]); 
data.push([4,"Test data",6]); 

app.use(function (req, res) { 
    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9000'); 
    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 
    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 
    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 
    res.send(data) 
    //next(); 
}); 

app.listen(9000, function() { 
    console.log('Example app listening on port localhost:9000!'); 
}); 

ich diese Daten in der HTML-Seite angezeigt werden sollen. Kann jemand hier mit dem HTML/JQuery-Skript helfen? Vielen Dank.

HTML-Code:

<script type="text/javascript"> 
jQuery(document).ready(function($) { 
alert('Hello'); 

$.ajax({ 
    url:"http://localhost:9000", 
    dataType: 'json', 
    data: data, 
    success: success 

}); 

$.get('/', {}, function(data){ 
// not sure it is the right code 
console.log(data); 
// then the code to create a list from the array (no pb with that) 
}); 

//function jsonpCallback(data){ 
// alert("jsonpCallback"); 
//} 
    //do jQuery stuff when DOM is ready 
}); 
</script> 

    <h1>Header Text</h1> 

Antwort

1

Zuerst haben api Routen einzurichten und eine File-Serving-Route. Dann können Sie die api-Methode (zB über jQuery ajax) aufrufen und die Antwortdaten erhalten.

+0

können Sie bitte ein Codebeispiel dafür teilen? –

0

Versuchen Sie stattdessen Ihre node.js Code:

var express = require('express'); 
var app = express(); 

var data = new Array;  
data.push([1,2,3]); 
data.push([4,"Test data",6]); 

app.get('/', function (req, res) { 
    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 
    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 
    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 
    res.send(data); 
}); 

app.listen(3000, function() { 
    console.log('Example app listening on port 3000!'); 
}); 

ich mich jetzt um dies zu testen nicht NodeJS haben, aber dies sollte Ihnen eine Idee geben. Sie müssen Routen definieren. Ich habe .get ("/") für Sie definiert, so dass jede Anfrage an die Haupt-URL diese Funktion aufruft. Ich habe auch Access-Control-Origin auf * gesetzt. Wenn Sie die Dinge ein wenig sichern wollen, sollten Sie statische Dateien von nodejs bereitstellen.

Verwandte Themen