【问题标题】:XMLHttpRequest cannot load error on accessing MEAN.js API through IONIC appXMLHttpRequest 在通过 IONIC 应用程序访问 MEAN.js API 时无法加载错误
【发布时间】:2016-01-09 20:06:00
【问题描述】:

我正在开发一个ionic 应用程序,试图访问MEAN.JS 应用程序的API,如下所示,它是编码JSON 的链接

https://damp-dawn-2579.herokuapp.com/api/foods

我的应用程序的工厂服务访问上面的链接却报错了

XMLHttpRequest cannot load http://localhost:3000/api/foods. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

工厂服务

.factory('Foods', ['$resource',
  function($resource) {
    return $resource('https://damp-dawn-2579.herokuapp.com/api/foods/:foodId', { foodId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
])

我该如何克服这个问题? 谢谢

【问题讨论】:

    标签: angularjs xmlhttprequest ionic-framework meanjs


    【解决方案1】:

    在节点中启用交叉请求

    app.all('/', function(req, res, next) 
    {
          res.header("Access-Control-Allow-Origin", "*");
          res.header("Access-Control-Allow-Headers", "X-Requested-With");
          next();
    });
    

    【讨论】:

      【解决方案2】:

      解决此错误的一个简单方法是在 google chrome 上添加扩展 Allow-Control-Allow-Origin:

      plugin link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        • 2017-03-02
        • 2016-09-06
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 2017-05-28
        相关资源
        最近更新 更多