客户端javascript没有'找不到我的网址

Client side javascript doesn't find my url

本文关键字:找不到 我的 javascript 没有 客户端      更新时间:2023-09-26

我想用nodejs和javascript/jquery开发一个客户端-服务器端,但我被卡住了。我有一个大表单,用户提交并将数据发送到/getData url,这非常有效。但我现在的问题是,当我想从/getData向我的客户端获取这些数据时。

这是我的客户端文件:

var client = {};
client.start = function () {
    client.getData();
};
client.cb_get = function () {
    var data={};
    if (this.readyState == 4 && this.status == 200) {   
        data= JSON.parse(this.responseText);
        alert("We get the data" + JSON.stringify(data, null, 4));
        client.chart(data);
    } else {
        alert("Sorry this page is not allow without processing any form");
    }
};

client.get = function(req, cb) {   
    var xhr = new XMLHttpRequest();
    xhr.open("GET", req, true);
    xhr.onreadystatechange = cb;
    xhr.send();
};

client.getData= function () { 
    var req="http://localhost:3000/getData";
    client.get(req,client.cb_get);
};
client.chart= function (data) {
  //Display data as charts using jquery in an other html page.
};
window.onload = setTimeout(client.start, 1);
HTMLElement.prototype.has_class = function (c) 
{
    return this.className.indexOf(c) >= 0;
};

但我一直有404错误,我不知道为什么。

我的服务器文件:

var express = require('express')
    bodyParser =require("body-parser");
    routes= require('./router.js');
var app= express();
app.use(express.static(__dirname + '/'));
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));

//define our routes
app.get('/', routes.index); //open home page
app.get('/simulation', routes.simulation);
app.get('/chartData', routes.chartData);
app.post('/getData', routes.getData);
//In case of malicious attacks or mistyped URLs
app.all('*', function(req, res){
  res.send(404);
})
var server = app.listen(3000, function () {
  var host = server.address().address
  var port = server.address().port
  console.log('Example app listening at http://%s:%s', host, port)
})

我的路由器文件:

module.exports.index= function(req, res){
    fs.readFile('index.html', function(err, page) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(page);
        res.end();
    });
};
module.exports.simulation= function(req, res){
     fs.readFile('simulation.html', function(err, page) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(page);
        res.end();
    });
};

module.exports.chartData= function(req,res) {
     fs.readFile('chartPage.html', function(err, page) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(page);
        res.end();
    });
};
module.exports.getData= function(req,res) {
    var data= {};
    data= req.body;
    res.send(JSON.stringify(data, null, 4));
    console.log(req.body);    
};

那么我错在哪里了?此外,当我提交时,我的/getdata页面会打开(由于在我的表单标记中指定了action= /getData,这是正常的),但我想直接打开带有图表的html页面。我该怎么做?

对不起,伙计们,我的帖子太长了,但我真的需要帮助。

您的ajax请求执行

xhr.open("GET", "http://localhost:3000/getData", true);

您的路线侦听

app.post('/getData', routes.getData);

请注意,您是如何发送GET请求和侦听POST请求的,这是不一样的,所以您最终会使用404路由。

您必须更改ajax请求并发送POST请求,或者路由并侦听GET请求。