【问题标题】:Error access-control-allow-origin on angularjs $http.postangularjs $http.post 上的错误访问控制允许来源
【发布时间】:2017-01-28 11:27:30
【问题描述】:

我用 angularjs 编写了简单的代码来与我之前托管在共享服务器中的 PHP 服务器 API 进行通信。我使用 $http.get 和 $http.post 从服务器获取数据,但它返回错误“access-control-allow-origin”,如下所示:

http://dpmp.arkoding.tk/index.php/api/datapost. Response to preflight request doesn't pass 
access control check: No 'Access-Control-Allow-Origin' header is present on the requested 
resource. Origin 'http://localhost:8100' is therefore not allowed access. 
The response had HTTP status code 404.

access-control-allow-origin

我得到的大多数参考资料都解释说它是由服务器站点上启用的 CORS 引起的,只需简单地添加一行 'header('Access-Control-Allow-Origin: *');'在顶部进行修复。我已经这样做了,但它只是修复了“$http.get”功能,但对于“$http.post”我仍然遇到同样的错误。这让我更加困惑,因为如果我使用邮递员并且我认为问题出在我的角度代码中,它会起作用。

我尝试在我的 services.js 中添加一些代码,如下面的 sn-ps 所示:

  var datac = {id: 'helo'};
  var head = {'Content-Type': 'application/json'};
  return $http({
      url: url + '/datapost',
      method: 'POST',
      headers: head,
      data: datac
  });

没有标题

  var datac = {id: 'helo'};
  return $http({
      url: url + '/datapost',
      method: 'POST',
      data: datac
  });

但它不会工作。我什至尝试将我的 rest API 移动到另一个托管服务提供商,当我使用 angular $http.post 时它也返回了相同的错误。 非常感谢您对我的问题的帮助, 顺便说一句,我很抱歉我的英语不整洁:)。

【问题讨论】:

  • 这是错误消息中最重要的位:响应的 HTTP 状态代码为 404

标签: javascript php angularjs rest cors


【解决方案1】:

解决了!终于... :D

基于this link,终于解决了问题..

谢谢...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-25
    • 2017-09-20
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    相关资源
    最近更新 更多