【问题标题】:calling API works fine through postman but posting via axios does not work通过邮递员调用 API 工作正常,但通过 axios 发布不起作用
【发布时间】:2018-10-20 17:36:49
【问题描述】:

我正在制作一个反应原生应用程序, 我注册了一个进行 API 调用的 headlessJS。 API 通过邮递员工作正常,但通过应用程序发出请求时,出现错误 这是我在应用程序中的请求代码

axios({
  method: 'post',
  url: '13.126.71.152:3000/checkin',
  data: {
      location,
      name: naming
  }
}).then((res) => {
  return res;
}).then((resp) => {
  console.log(resp);
}).catch((error) => {
  console.log(error);
});

我收到以下错误

Error: Network Error
at createError (20ecc2c2-cb5e-4e50-9…-5e92abf47ba8:80610)
at XMLHttpRequest.handleError (20ecc2c2-cb5e-4e50- 
9…-5e92abf47ba8:80518)
at XMLHttpRequest.dispatchEvent (20ecc2c2-cb5e-4e50-9…-5e92abf47ba8:17922)
at XMLHttpRequest.setReadyState (20ecc2c2-cb5e-4e50-9…-5e92abf47ba8:17677)
at XMLHttpRequest.__didCompleteResponse (20ecc2c2-cb5e-4e50-9…-5e92abf47ba8:17504)
at 20ecc2c2-cb5e-4e50-9…-5e92abf47ba8:17614
at RCTDeviceEventEmitter.emit (20ecc2c2-cb5e-4e50-9…f-5e92abf47ba8:3478)
at MessageQueue.__callFunction (20ecc2c2-cb5e-4e50-9…f-5e92abf47ba8:2384)
at 20ecc2c2-cb5e-4e50-9…f-5e92abf47ba8:2154
at MessageQueue.__guardSafe (20ecc2c2-cb5e-4e50-9…f-5e92abf47ba8:2346)

我在节点 js 上的服务器代码 我的 server.js 代码

    var express = require('express');
    app = express();
    const cors = require('cors');
    const path = require('path');
    port = process.env.PORT || 3000,
    bodyParser = require('body-parser');

    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    // Set public folder
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(function(req, res, next) {
       res.header("Access-Control-Allow-Origin", "*");
       res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,x-auth");
       res.header('Access-Control-Expose-Headers', 'x-auth');
       next();
    });

    var routes = require('./api/routes/MetroRoutes'); //importing 
route
    routes(app); //register the route

    app.listen(port);

    app.use(function(req, res) {
      res.status(404).send({url: req.originalUrl + ' not found'})
    });
    console.log('metro RESTful API server started on: ' + port);

我的控制器代码:./api/controllers/MetroController

    exports.checkInDetected = function(req,res) {
      console.log(req.body);
      pusher.trigger( 'ieacon-calc', 'checkInDetected', {
      "name": req.body.name,
      "location": req.body.location
      });
      res.json({ message: 'checkInDetected' });

    }

    exports.checkOutDetected = function(req,res) {
      pusher.trigger( 'ieacon-calc', 'checkOutDetected', {
      "name": req.body.name,
      "location": req.body.location
      });
      res.json({ message: 'checkOutDetected' });
    }

这是我的路线文件:./api/routes/MetroRoutes

module.exports = function(app) {
      var todoList = require('../controllers/MetroController');

      // todoList Routes
      app.route('/checkin')
        .post(todoList.checkInDetected)

      app.route('/checkout')
        .post(todoList.checkOutDetected);
    };

【问题讨论】:

  • 检查网址。可能是因为缺少 http 或 https
  • 我刚刚使用了AWS@Revansiddh提供的insance IP
  • 您需要传递http://13.126.71.152:3000/checkin 并确保您已经定义了/checkin 发布路线
  • @ArifKhan 浏览器无法访问此网址,并且我在 App 上收到 404 状态错误
  • 这就是我告诉你的原因,确保你已经定义了/checkin post route,你也可以提供./api/routes/MetroRoutes code

标签: node.js express react-native fetch axios


【解决方案1】:

添加 app.use(cors()); 它应该可以工作。

如果使用节点创建 API,请始终使用 cors。 阅读 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS 了解 CORS

【讨论】:

  • 试过没有成功:/
猜你喜欢
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多