Kageyasha
Kageyasha

Reputation: 31

ExpressJS and http(ajax?) call doesnt work?

im trying to do a simple ajax call but somehow it doesnt work.I cant figure it out, would be nice if someone could help me.

Server script :

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

//allow to use static files
app.use(express.static("public"));
//listen to smth
app.get('/test1', function (req, res) {
    res.send('GET request to the homepage');
  });
//start server

app.listen(port);
console.log("Server running on port" + port);

HTML Button that runs a client based JS

<button onclick="callServerSideScript('key','port','address','username','password','gem','proxy','crypt')" type="button" id="build">Get build</button>

Client based JS :

function callServerSideScript(key,port,address,username,password,gem,proxy,crypt){
    keyVal = document.getElementById(key).value;
   portVal = document.getElementById(port).value;
    addressVal = document.getElementById(address).value;
    userVal = document.getElementById(username).value;
    pwVal = document.getElementById(password).value;
    gemVal = document.getElementById(gem).checked;
    proxyVal = document.getElementById(proxy).checked;
    crytpVal = document.getElementById(crypt).checked;

    httpRequest = new XMLHttpRequest();
    httpRequest.open('POST', '/test1');
    httpRequest.send('some data');         
}

Normally it should do a /test1 request to the server and the server should react to it? I am missing something?

Upvotes: 0

Views: 30

Answers (1)

Tobias Fuchs
Tobias Fuchs

Reputation: 938

eighter do httpRequest.open('GET', '/test1'); (client) or app.post('/test1', handler); (server). but if your sending a POST request and the server expects a GET request it just gets 404'd

Upvotes: 1

Related Questions