Get response from node js in ionic html page

For example, when having a successful registration, get a message on the page “Thanks for registering”.

the code I’m using:
index.js

if(results.affectedRows>0)
{
console.log(“1 record inserted”);
res.send({
passed: true,
message: ‘Thanks for registering!’
});
}

ts file
message:string;

senduserdata(){
var dataToSend = {
username:this.inputusername,
password:this.inputpass,
fname:this.inputfname,
lname:this.inputlname,
email:this.inputemail,
}
var url = ‘http://localhost:3000/insertuser’;
this.http.post(url,{data:JSON.stringify(dataToSend)},
{headers:new HttpHeaders(
{“content-Type”:“application/json”})}).subscribe(
(data)=>{
alert(data);
}, (res)=>{
this.message= res.message;
}
)
}

.html page
<ion-input [(ngModel)]=“message” style=“font-family: ‘Poppins’, sans-serif;”>

Thnx for the time.

Your question may require a bit more elaboration, but from the face of it I think this piece of code won’t lead to a succesful post to a node.js server. Unless localhost is running that server - but I guess this you may not have done and you are implicitly expecting?

Thanks for the reply, the localhost is running that server, and also the registration is completed but there is no response in the HTML page. And this console.log(“1 record inserted”); also works is just the res. send that doesn’t work. I think that I must be doing something wrong in the ts. file code but can’t figure out what. I get a message that says “Object object” on the HTML page.

If you see this in html, you may want to inspect the same value in console.log. As this means that the object you are trying to present needs better referencing - assuming it does have the data you need

“Http failure during parsing for http://localhost:3000/insertuser
I get this as message in html
And
“User created in db!” In console log

Code in nodejs file
if(results.affectedRows>0)
{
console.log(‘User created in db!’);
res.send(‘User created in db!’);
}

Any idea?

What do you get if you put the url in your browser? Do you get valid json?

I don’t. Its a Post request. Should I be getting anything?

Apologies. I should have known…

?

You use application/json as content type… Is that really correct? I don’t use post a lot, so would have to look that up.

Next I believe json is default, so i wonder if u need to specify at all

You were right that “{“content-Type”:“application/json”}” was wrong. Thanks again! :slight_smile:

1 Like

The server is always the ultimate arbiter of what constitutes “correct” for a given request, but yes, “application/json” is a viable, proper content type, and, furthermore,

It is, and one should not be manually stringifying JSON or manually setting it as a content type. If you feed an object or a number as the body to an HttpClient request, the content type is automatically set to application/json. Similarly, it is set to application/x-www-form-urlencoded if you feed HttpParams, text/plain if you feed a string. The code that does this is here.

2 Likes