I’m making a desktop application using electron. When calling one of my post requests in my index.js file under “app.whenReady()”, i get the status code, hostname of the post request that I’m making which is cool.
The problem I’m having is calling a post request when clicking on one of the buttons within my html index file.
Can someone please clarify what i may be doing wrong- any extra functions or methods that i may need to add in the html template?
Below is the function that I’m currently calling on my app start up (this works fine as its giving me something back within the terminal)
function Connect(server, sId, aId, password) {
console.log("Creating connection")
var postData = {Server: server, SId: sId, AId: aId, Password: password}
var strData = JSON.stringify(postData);
// creating Json web token
const token = jwt.sign({strData}, process.env.TOKEN_SECRET, {algorithm: "HS512"}, {expiresIn: 30000});
MakeRequest('connect', strData);
}
Below is the code for the post request:
function MakeRequest(path, data) {
const options =
{
method: 'POST',
protocol: 'https:',
hostname: '',// I do have a hostname but left it empty
path: `${path}`,
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Content-Length': 89,
'authorisation': ''
}
};
const request = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
})
response.on('end', () => {
console.log('No more data in response.')
});
});
request.on('error', error => {
console.error(error)
})
request.write(data, 'utf-8');
request.end();
}
Below is where I would like to call one of my post requests:
<div id="btnlogin" class="standard-button" @onclick="" onmouseenter="hoverMouseOver(event)" onmouseleave="hoverMouseLeave(event)">
<img src="img/logon.svg" height="100%" width="100%">
<span class="tooltiptext">Log on</span>
</div>
So far I have loaded the script within the head of the template:
<script src="commands.js"></script>
I have then tried to call this method using the onclick method but im not getting anything back through the terminal. If i insert an alert within the function im calling, that works fine but the post request just isnt working for some reason.
Can someone please help? Thankyou!!