0

Im using angular 6 & I have never used angular before. I need to send data to nodejs server. Im using the following code in angular function

     import { Component, OnInit } from '@angular/core';
     import { HttpClient,HttpHeaders  } from '@angular/common/http';

      const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
       };


     fun_add(Id, cat) {
     return this.http.post('/go', data, 
      httpOptions).subscribe(result => {
        console.log(result);
       }, error => console.log('There was an error: '));
      }

In my nodejs,

    app.post('/go', function (req, res) {
      console.log("hi");
      })

But I couldnt go to the server. Im getting the error msg 'there was an error'. can anyone please help me with this?

19
  • 1
    http.post('http://localhost:port/go' otherwise it is no valid URL
    – user4676340
    Commented Aug 1, 2018 at 12:03
  • It should be valid, can you show us network traffic?
    – Akxe
    Commented Aug 1, 2018 at 12:06
  • @trichetriche — Relative URLs have been supported since before I'd even heard of the WWW.
    – Quentin
    Commented Aug 1, 2018 at 12:08
  • @Quentin that's cool and all, but Angular probably expects HTTP url and not relative ones. Besides, Using /go isn't really that relative, it will try to get the root of whatever the folder is ...Not only that's not very clear, but that's also very random
    – user4676340
    Commented Aug 1, 2018 at 12:10
  • 1
    @trichetriche — Angular and the underlying XMLHttpRequest object it calls runs in the JavaScript environment hosted by a webpage. Relative URLs are relative to the URL of that page.
    – Quentin
    Commented Aug 1, 2018 at 12:12

3 Answers 3

1

You never send a response (e.g. with res.send()), so the server gets the request and sits there.

Meanwhile, the client waits, and waits, and eventually times out and throws an error because the server never responded.

8
  • If it was the case he would have seen the result of console.log("hi");
    – user4676340
    Commented Aug 1, 2018 at 12:12
  • @trichetriche — They only reported what they saw in the browser.
    – Quentin
    Commented Aug 1, 2018 at 12:13
  • @trichetriche — There is no statement, one way or the other, as to what the output on the console from Node.js is. That is a fact, not an assumption.
    – Quentin
    Commented Aug 1, 2018 at 12:14
  • My bad, there was an error is the error caught. Removing my previous comment !
    – user4676340
    Commented Aug 1, 2018 at 12:16
  • @quentin I have tried sending response too. Still its not working. Commented Aug 1, 2018 at 12:17
0
    let api_url = 'http:localhost:portNumber'

   fun_add(Id, cat) {
      return this.http.post(api_url+'/go', data, 
          httpOptions).subscribe(result => {
            console.log(result);
       }, error => console.log('There was an error: '));
    }
2
  • Are you able you to hit API? @BhavaniSankar Try sending response back as res.status(200); res.json({ success: "Data" }); Commented Aug 1, 2018 at 12:23
  • still no.. Is there any other way to send datas from angular to nodejs? Vishal Hasnani Commented Aug 1, 2018 at 12:45
0

Add this code in component

constructor(private http: HttpClient);

It's Working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.