【问题标题】:Magento2 REST API with AngularJS and IonicMagento2 REST API 与 AngularJS 和 Ionic
【发布时间】:2016-08-24 12:55:12
【问题描述】:

我正在使用 Ionic、Firebase、AngularJS 开发一个项目,并尝试从一个 magento2 项目中检索数据,该项目在 ubuntu 14.04 中使用 apache2 (http://localhost/magento2/) 在本地托管。

我解决了 CORS 错误,将其添加到我的 .htaccess 文件中

<IfModule mod_headers.c>

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, Authorization, X-Requested-With, Content-Type, Accept"

</IfModule>

用于检索项目的 AngularJS product.js 文件

function getProducts() {
  $http({
    method: 'GET',
    url: 'http://localhost/magento2/rest/V1/products/24-MB01',
    headers: {
      'Authorization': 'Bearer vbasmelg29r94nf8u10o6b8cxoko5rn9'
    }
  }).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    console.log('[HTTP GET SUCCESS]', response);
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    console.log('[HTTP GET ERROR]', response);
  });
}

我使用 Postman 测试了这些 HTTP 调用并返回状态 200 OK。当我尝试通过角度使用时,我遇到了来自 chrome 的输出:

OPTIONS http://localhost/magento2/rest/V1/products/24-MB01 ionic.bundle.js:25000
XMLHttpRequest cannot load http://localhost/magento2/rest/V1/products/24-MB01. Response for preflight has invalid HTTP status code 400 :8100/#/product:1

拜托,你能帮帮我吗?

【问题讨论】:

  • 您好,您是如何解决问题的?我已经放弃了对这个问题的投票。

标签: angularjs rest ionic-framework magento2


【解决方案1】:

如果您使用 Chrome 来测试您的 Angular 应用程序,那么旧版本(大约 51.x)在预检请求方面存在这些问题。升级应该可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 2017-02-19
    • 2017-04-06
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2017-08-21
    相关资源
    最近更新 更多