【问题标题】:Access-Control-Allow-Origin' Issue on angularjs appangularjs应用程序上的Access-Control-Allow-Origin'问题
【发布时间】:2017-10-20 07:17:02
【问题描述】:

XMLHttpRequest 无法加载 domain.com/contact-form.php。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'domain.com:81'。

【问题讨论】:

    标签: angularjs


    【解决方案1】:
     app.config.js (your angular config file)
    
               $httpProvider.defaults.headers.post["Content-Type"] = 'text/plain';
               $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    
    config.php (your php config file)
              header('Access-Control-Allow-Origin: *');
              header("Content-Type: text; charset=UTF-8");
              header("Access-Control-Allow-Methods: POST");
              header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
              ini_set('display_errors', 'On');
              error_reporting(1);
    

    【讨论】:

      【解决方案2】:

      在所有代码之前将此标头添加到您的 PHP 文件中。

      header("Access-Control-Allow-Origin: *");
      header("Content-Type: text; charset=UTF-8");
      header("Access-Control-Allow-Methods: POST");
      header("Access-Control-Max-Age: 3600");
      header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
      

      希望它能解决问题。

      【讨论】:

      • 我仍然收到错误:跨域请求被阻止:同源策略不允许读取 domain.com/contact-form.php 上的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。
      猜你喜欢
      • 2017-09-09
      • 2016-04-22
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2016-09-20
      • 2014-06-07
      • 2023-02-14
      • 2015-09-19
      相关资源
      最近更新 更多