繁体   English   中英

http get vue.js + node.js返回空数据

[英]http get vue.js + node.js returns empty data

我正在用vue + nativescript做一个项目

从vue项目调用函数app.get时不会触发

这个电话:

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

返回:“ data”:“”,“ status”:null,“ statusText”:“”就像服务器关闭一样(( 调用本身有效,它可以与其他api一起使用

但是在浏览器上使用angularjs进行的简单测试会返回有效的所需数据

这是我的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
})

res.end()用于结束无数据的响应(请参阅https://expressjs.com/en/api.html )。

如果要返回json,最简单的方法是使用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);
});

我发现了问题,这是ios的安全问题,它们不允许http调用,仅允许https(我在ios模拟器上运行项目)

无法加载资源,因为“应用程序传输安全性”策略要求使用安全连接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM