简体   繁体   English

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

[英]Error: No 'Access-Control-Allow-Origin' header is present on the requested resource

I have two systems helpdesk.ops.something.in and dev1.ops.something.in 我有两个系统helpdesk.ops.something.indev1.ops.something.in

I have a file fetchP.php in helpdesk.ops whose code goes something like this: 我在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>';

which is doing a GET request to a file createurl.php present in the dev1.ops, which goes something like this: 这是对createurl.php存在的文件createurl.php进行GET请求,如下所示:

<?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: 但在执行时,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.

What am I missing? 我错过了什么?

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 ). 即使设置了Access-Control-Allow-Origin标头,XMLHttpRequest也不能在与您当前域不同的域上请求资源(这是由于同源策略 )。

One way you could try to get around it is to use JSONP . 你可以尝试解决它的一种方法是使用JSONP Here's a simple and rudimentary example: 这是一个简单而简单的例子:

fetchP.php (Ajax call): 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 : 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: 在ajax请求上输出createurl.php示例:

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). 然后jQuery执行定义的函数并在给定的参数上调用定义的success方法(在本例中为JSON)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No ‘Access-Control-Allow-Origin’ header is present on the requested resource PHP:请求的资源上不存在“Access-Control-Allow-Origin”标头 - PHP : No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源 Symfony 上不存在“访问控制允许来源”header - No 'Access-Control-Allow-Origin' header is present on the requested resource Symfony Laravel 7 - 没有“访问控制允许来源” header 存在于请求的资源上 - Laravel 7 - No 'Access-Control-Allow-Origin' header is present on the requested resource ''Access-Control-Allow-Origin' header 出现在请求的资源上 - ''Access-Control-Allow-Origin' header is present on the requested resource WordPress 请求的资源上不存在“Access-Control-Allow-Origin”标头 - WordPress No 'Access-Control-Allow-Origin' header is present on the requested resource “向Jawbone UP API发送请求时,”请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头“错误 - “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when sending request to Jawbone UP API Orgchart js 错误:请求的资源上不存在“Access-Control-Allow-Origin”标头 - Orgchart js error : No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM