gpt4 book ai didi

javascript - 客户端 javascript 找不到我的网址

转载 作者:行者123 更新时间:2023-11-30 12:26:32 25 4
gpt4 key购买 nike

我想用 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 请求。

关于javascript - 客户端 javascript 找不到我的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29195532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com