0

I'm doing a project with vue + nativescript

the function app.get is not triggerd when I'm calling it from the vue project

this call :

const urlChannels = 'http://localhost:3001/sources';
  axios.get(urlChannels)
   .then(response => {
     store.commit('setTasks', {
       channels: response.data,
     });
   }) 
 }

returns :"data":"","status":null,"statusText":"" as if the server is off,(the call itself is valid it works with other apis)

but simple test with angularjs on the browser returns the valid needed data

this is my nodejs :

app.get('/sources', function (req, res) {
  res.set({
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Methods': 'GET'
});
  res.writeHead(200,{'Content-Type':'application/json'})
  let data = getNews.getSources()
  res.send(JSON.stringify(data));
  //res.json(data); also tried this same resualt
})
Nadav
  • 333
  • 3
  • 16
  • Why are you using res.end() instead of res.send()? – Jim B. Oct 27 '18 at 20:33
  • Also, are you sure urlChannels has the correct url? Can you show us how that's generated? – Jim B. Oct 27 '18 at 20:38
  • I've tried all sorts of option before the res.end.. it is important to mention that a simple ajax call from a test request in angularjs does bring the data succesfully updated the question – Nadav Oct 28 '18 at 09:39
  • What does getNews.getSources() return? – Jim B. Oct 28 '18 at 21:49
  • 1
    I found the problem, it's a security thing with ios, they don't allow http calls, only https (I run the project on an ios emulator) – Nadav Oct 30 '18 at 07:45

2 Answers2

0

res.end() is intended for ending the response without data (see https://expressjs.com/en/api.html).

If you want to return json, the easiest way is to use res.json():

app.get('/sources', function (req, res) {
    res.set({
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET'
    });
    let data = getNews.getSources()
    res.json(data);
});
Jim B.
  • 4,512
  • 3
  • 25
  • 53
0

I found the problem, it's a security thing with ios, they don't allow http calls, only https (I run the project on an ios emulator)

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

Nadav
  • 333
  • 3
  • 16