Magento 跨域使用来自其他域的 ajax 发布 fething 数据


magento crossdomain issue fething data using ajax from others domains

在magento中,我使用了视频而不是产品图像,这是一个市场。 前端有四种类型的用户1. 卖家2. 公司3. 业务代理4. 网站客户

业务代理 ->业务代理向其他网站提供所有卖方产品,如果通过其他网站购买,他将获得佣金,所以我向Business Agento提供嵌入式代码,以便他们可以将其实施到其他网站,但是当我尝试从其他域击中Ajax时,它显示跨域错误。

Cross-Origin Request Blocked: 
The Same Origin Policy disallows reading the remote resource at http://shopahol.com/demo/test.php?atoken=c0Q5QUk1VUgreEk9. 
(Reason: CORS header 'Access-Control-Allow-Origin' missing). 

为了解决这个问题,我尝试将访问控制允许标头添加到索引中.php

header('Access-Control-Allow-Origin: *');

但问题没有解决同样的错误。 然后我尝试将跨域.xml放在我的根文件夹中

<!--l version="1.0-->
<!--pan class="hiddenSpellError" pre=-->
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="*" />
</cross-domain-policy>

它也失败了同样的问题,任何人都可以有任何解决方案或我做错了什么。

这是我在其他网站上显示视频的代码。(简单的HTML或PHP文件)

 <!-- html starts -->  
        <div id="tevid" style="width:100%;">
            <video id="homevideo" width="100%" autoplay onended="run()"></video>
        <div class="bg-background-new">
        <a href="" class="product-name-new" id="product-name">Product Name</a>
        <div class="price-new" id="price"></div> 
        <a href="" id="button1" class="new-button">Buy Now</a>
        </div> 
        </div>
        <!-- javascript starts *Remove this script if you already included ajax jquery  -->  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" >  </script>
        <script>
          var video_count =0;
          var videoPlayer = document.getElementById("homevideo");
          var allproduct = [];
          function getvideos(){ 
                $( document ).ready(function() {
                $.ajax({
                        url: "http://shopahol.com/demo/test.php?atoken=c0Q5QUk1VUgreEk9",
                        type: "GET",
                        dataType: "json", 
                        success: function(data){ for(var i in data) { allproduct.push(data[i]);  } run(); },
                        error: function() { alert("Error During Process"); }    
                });
                 });
          }
          function run(){ 
            var nextVideo = allproduct[video_count].videourl;
            var price = allproduct[video_count].pric;
            var name = allproduct[video_count].name;
            var url = allproduct[video_count].url;
            videoPlayer.src = nextVideo;
            videoPlayer.play();
            video_count++;
            $("#price").html("$ "+price);
            $("#product-name").html(name);
            $("#product-name").attr("href", url)
            $("#button1").attr("href", url)
            if(allproduct.length == video_count){ video_count = 0; }
          }
         $( document ).ready(function() {
            getvideos() ;
         });
        </script>
        <!-- css starts -->  
        <style>
        a.new-button {
          background: red none repeat scroll 0 0;
          border: medium none;
          bottom: 0;
          color: #fff;
          cursor: pointer;
          float: right;
          font-weight: bold;
          margin-top: 10px;
          padding: 10px 20px; float: right;
        }
        .price-new {
          color: #000;
          display: block;
          float: right;
          line-height: 35px;
          padding: 10px;
        }
        a.product-name-new {
          color: #000;
          display: inline-block;
          float: right;
          line-height: 35px;
          margin: 10px 0;
          position: relative;
          right: 2px;
          text-decoration: none;
        }
        .bg-background-new {
          background: transparent none repeat scroll 0 0;
          bottom: 65px !important;
          display: block;
          position: relative;
          right: 0 !important;
          width: 99%;
        }
        </style>

在帮助赞赏提前致谢

我想通了,所以我在相同情况下为其他人的帮助发布答案我更改了我的 ajax 函数

function getvideos(){ 
                $( document ).ready(function() {
                $.ajax({
                        url: "http://shopahol.com/demo/test.php?atoken=WkFBU2F6bFdCNFU9",
                        type: "GET",
                        crossDomain: true,
                        dataType: "jsonp",
                        jsonpCallback: 'giveBacktome',                          
                        error: function() { console.log("Error During Process"); }  
                });
                 });
          }

它有效

通过向 .htaccess 或 apache/IIS 等添加规则来解决问题:CORS 标头(DevOps 服务器配置问题)。

例如,在 .htaccess 中允许所有域:

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>

或 foo-domain.com:

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "foo-domain.com"
</IfModule>