3

I'm using http.get to get data from nodejs to angular. I want load some content on page loading. So, I'm just calling it in initialize method.

_initialize(): void {
this.http.get('http://127.0.0.1:3000/query/getId').subscribe(
  data => {
    console.log("success");
  },
  err => {
    console.log("Error occured."+JSON.stringify(err));
  }
);
}

In my server js,

app.get('/query/getId',function(req,res){
             console.log("hi there");

 })

I want to pass the data, but as of now the console message itself not displaying in node. And in browser I could see the message error occured. message":"Http failure during parsing for the url" . Can anybody tell me how to proceed with this?

4
  • That's because Angular expects to get JSON back from your server, but at the moment it doesn't. If you modify your server to send back some basic JSON (even just an empty object like res.send('{}')) then that error should go away
    – user184994
    Commented Aug 5, 2018 at 6:24
  • still getting the same error message":"Http failure during parsing for the url" @user184994
    – viki
    Commented Aug 5, 2018 at 6:38
  • If you check in the browsers developer tools (F12) and check the network tab for the request, can you see the response that's coming back?
    – user184994
    Commented Aug 5, 2018 at 6:41
  • status code is 200 & in response tab "your browser doesnt support javascript" @user184994
    – viki
    Commented Aug 5, 2018 at 7:28

3 Answers 3

0

You can specify that the data to be returned is not a JSON using the responseType. See the Requesting non JSON data

In your example, you should be able to use:

 this.http.post('http://127.0.0.1:3000/query/getId',{}, {responseType: 'text'})
4
  • I want json response only.. But after returning json data also I'm getting same error like "message":"Http failure during parsing for the url" .
    – viki
    Commented Aug 5, 2018 at 6:41
  • ok in that case set the headers to application json and inspect your network tab what is being sent and recieved Commented Aug 5, 2018 at 6:42
  • In network tab. Im getting status code 200. But still I'm facing he same error.
    – viki
    Commented Aug 5, 2018 at 7:19
  • are you using angular 6 with httpclient? Commented Aug 5, 2018 at 7:21
0

Add on node function:

res.json("hi there");
1
  • still getting the same error like "message":"Http failure during parsing for the url"
    – viki
    Commented Aug 5, 2018 at 6:39
0

Try the following which uses JSON data:

app.get('/query/getId',function(req,res){
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ a: 1 }));
 });
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.