dream5694 2015-11-17 06:47
浏览 440
已采纳

错误:请求的资源上没有“Access-Control-Allow-Origin”标头

I have two systems helpdesk.ops.something.in and dev1.ops.something.in

I have a file fetchP.php in helpdesk.ops whose code goes something like this:

<?php
header('Access-Control-Allow-Origin: *');
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
    function someFunc(item) {
        $.ajax({method:"GET", 
        url:"http://dev1.ops.something.in/wallet/createurl.php?phone="+item, 
        success:function(response){
            console.log(response);
        }
        });
    };
</script>';
<?php
echo '<div id="callToWallet" class="sample-button" onclick="someFunc(911234567890);"><a href="#"> Click here</a></div>';

which is doing a GET request to a file createurl.php present in the dev1.ops, which goes something like this:

<?php
header('Access-Control-Allow-Origin: *');?>
<script>response.addHeader("Access-Control-Allow-Origin", "*");</script>
<?php 
// the rest of the code
?>

But on executing, the GET request is not successful, and I am getting error:

XMLHttpRequest cannot load http://dev1.ops.something.in/wallet/createurl.php?phone=911234567890. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://helpdesk.ops.something.in' is therefore not allowed access. The response had HTTP status code 500.

What am I missing?

  • 写回答

1条回答 默认 最新

  • douyanlu7380 2015-11-17 07:16
    关注

    Even with the Access-Control-Allow-Origin header set, a XMLHttpRequest cannot request ressources on domains that are different from the one of your current domain (this is due to the same-origin policy).

    One way you could try to get around it is to use JSONP. Here's a simple and rudimentary example:

    fetchP.php (Ajax call):

    function someFunc(item) {
        $.ajax({
            method: "GET",
            data: { phone: item },
            url: "http://localhost:2512/createurl.php", 
            success: function(response){
                console.log(response);
            },
            dataType: "jsonp",
        });
    };
    

    createurl.php:

    <?php
      header('Access-Control-Allow-Origin: *');
    
      $data = ["foo" => "bar", "bar" => "baz"];
      $json = json_encode($data);
    
      $functionName = $_GET['callback'];
    
      echo "$functionName($json);";
    ?>
    

    Example output of the createurl.php on an ajax request:

    jQuery2130388456100365147_1447744407137({"foo":"bar","bar":"baz"});
    

    jQuery then executes the defined function and calls the defined success method on the given parameters (JSON in this case).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试