Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
客户端javascript不';找不到我的url_Javascript_Node.js_Client Server - Fatal编程技术网

客户端javascript不';找不到我的url

客户端javascript不';找不到我的url,javascript,node.js,client-server,Javascript,Node.js,Client Server,我想用nodejs和javascript/jquery开发一个客户机-服务器端,但我被卡住了。 我有一个大表单,用户提交数据并发送到/getDataurl,这非常有效。但我现在的问题是,当我想从/getData获取这些数据到客户端时 这是我的客户端文件: var client = {}; client.start = function () { client.getData(); }; client.cb_get = function () { var data={};

我想用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
页面会打开(由于在表单标记中指定了
操作=/getdata
,这是正常的),但我想直接打开带有图表的html页面。我该怎么做

很抱歉我的长篇大论,但我真的需要帮助。

您的ajax请求确实需要帮助

xhr.open("GET", "http://localhost:3000/getData", true);
你的路线在倾听

app.post('/getData', routes.getData);
注意你是如何发送一个
GET
请求和监听一个
POST
请求的,这不是一回事,所以你最终选择了404路径


您要么更改ajax请求,然后发送一个
POST
请求,要么路由并侦听
GET
请求。

我会使用小写字母表示URL,而不是camelcase?我总是使用camelcase…更改了它,但我的问题仍然存在。嘿,谢谢您的快速响应!我已经通过xhr.open更改了我的ajax请求(“POST”,“req”,true);但现在我的浏览器正在运行,直到它告诉我Safari无法打开页面!然后尝试在路由中返回一些简单的内容,比如
res.end('{“test”:“test”}')
并尝试在
cb_get
函数中记录
this
,有东西告诉它不是XHR对象,但我不太确定?我不确定确切原因,但一般来说,当浏览器挂起时,它会点击路由,但不会得到响应,所以它会一直尝试,直到超时。当它不在路线上时,你会直奔“一网打尽”路线。