【问题标题】:Error: No 'Access-Control-Allow-Origin' header is present on the requested resource错误:请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2015-11-17 06:47:27
【问题描述】:

我有两个系统helpdesk.ops.something.indev1.ops.something.in

我在 helpdesk.ops 中有一个文件 fetchP.php,其代码如下所示:

<?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>';

它正在对 dev1.ops 中的文件 createurl.php 发出 GET 请求,如下所示:

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

但在执行时,GET请求不成功,我收到错误:

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.

我错过了什么?

【问题讨论】:

标签: php


【解决方案1】:

即使设置了Access-Control-Allow-Origin 标头,XMLHttpRequest 也无法请求与当前域不同的域上的资源(这是由于same-origin policy)。

您可以尝试绕过它的一种方法是使用JSONP。这是一个简单的基本示例:

fetchP.php(Ajax 调用):

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);";
?>

createurl.php 在 ajax 请求上的示例输出:

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

jQuery 然后执行定义的函数并在给定的参数(本例中为 JSON)上调用定义的 success 方法。

【讨论】:

  • 那么如果我使用JSONP,即使两个php文件不在同一台机器上,它是否也能工作?
  • 应该,这就是 JSONP 的重点。当我尝试这段代码时,从技术上讲,createurl.php 中的标头甚至都不需要。
  • 但它在这里不起作用。它正在点击http://dev1.ops.something.in/wallet/createurl.php?callback=jQuery2130056362116476520896_1447747236866&amp;phone=911234567890&amp;_=1447747236867,并在控制台中显示错误:Failed to load resource: the server responded with a status of 500 (Internal Server Error)。我的createurl.php 是这样的:paste.kde.org/pqnivzx9r
  • 错误 500 表示您的服务器有问题 dev1.ops.something.increateurl.php。您的代码在我手动运行时有效。但请记住,您需要输出一个 javascript 函数才能使 JSONP 工作。
  • 请再看看我的回答。当通过 JSONP 进行 AJAX 调用时,jQuery 会附加几个参数,包括回调的名称(在您的评论中,这将是 jQuery21300563621164‌​76520896_1447747236866)。因此,您需要 createurl.php 的输出成为此函数,并带有包含您的数据的参数:jQuery21300563621164‌​76520896_1447747236866({"response":"okay","phonenumber:"911234567890"});。然后 jQuery 将解析结果并使用此参数执行您定义的 success 函数。
猜你喜欢
  • 2015-07-29
  • 2017-12-16
  • 2016-11-25
  • 2015-04-04
  • 1970-01-01
  • 2020-11-08
  • 2013-11-29
  • 2014-07-28
相关资源
最近更新 更多