简体   繁体   English

使用不同的URL加载时,请求的资源上没有“ Access-Control-Allow-Origin”标头

[英]Javascript No 'Access-Control-Allow-Origin' header is present on the requested resource on load of different url

I am trying to load value from different url to a div, When that is loaded it gives me an error. 我正在尝试将值从不同的url加载到div,加载时给我一个错误。 I tried to google a lot. 我尝试了很多谷歌。 I didn't find a fix. 我没有找到解决方法。 I am aware that this question is already asked but still nothing solved my problem. 我知道已经问过这个问题了,但仍然没有解决我的问题的方法。

XMLHttpRequest cannot load http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.w3schools.com' is therefore not allowed access.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#div1").load("http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=");
    });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>

The response you get from http://bulksms.icubetech.com/api/checkbalance.php should include the Access-Control-Allow-Origin in it's header. 您从http://bulksms.icubetech.com/api/checkbalance.php获得的响应应在标题中包含Access-Control-Allow-Origin

It should be something like: 应该是这样的:

Access-Control-Allow-Origin: yoursite.com

You can't access this site via ajax if the above requirement is not fulfilled. 如果不满足上述要求,则不能通过ajax访问此站点。


Alternatively, you can use a proxy php script to get this done. 或者,您可以使用代理php脚本来完成此操作。 The idea is to get a php script in your own domain to talk to the site you are trying to access and give you the result. 这个想法是在您自己的域中获取一个php脚本,以与您尝试访问的站点进行对话并为您提供结果。 This will not raise any cross domain issues since your browser communicates only with a php script in your own domain. 这不会引起任何跨域问题,因为您的浏览器仅与您自己域中的php脚本通信。 Here is a sample proxy script. 这是一个示例代理脚本。 Please change it to suite your needs. 请根据您的需要进行更改。

<?php

// Allowed hostname
define ('HOSTNAME', 'http://Your_Server/');

//returns the headers as an array
function getHeaders()
{
    $headers = array();
    foreach ($_SERVER as $k => $v)
    {
        if (substr($k, 0, 5) == "HTTP_")
        {
            $k = str_replace('_', ' ', substr($k, 5));
            $k = str_replace(' ', '-', ucwords(strtolower($k)));
            $headers[$k] = $v;
        }
    }
    return $headers;
} 

// Get the REST call path from the AJAX application
$path = $_REQUEST['path'];
$url = HOSTNAME.$path;

// Open the Curl session
$session = curl_init($url);

// If it's a POST, put the POST data in the body
if ($_POST['path']) {
 $postvars = '';
 while ($element = current($_POST)) {
  $postvars .= key($_POST).'='.$element.'&';
  next($_POST);
 }
 $postvars = substr($postvars, 0, -1);  // removing trailing &
 $postvars = str_replace('xml_version', 'xml version', $postvars);  // fix xml declaration bug?
 $postvars = stripslashes($postvars);

 curl_setopt ($session, CURLOPT_POST, true);
 curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}
else {
 //If it's a post, but not a form post (like a SOAP request)
 if ($_SERVER['REQUEST_METHOD']==='POST') {
  curl_setopt ($session, CURLOPT_POST, true);
  curl_setopt ($session, CURLOPT_POSTFIELDS, $HTTP_RAW_POST_DATA);

  $headers = getHeaders();
  $header_array = Array( "Content-Type: text/xml", "SOAPAction: " . $headers['SOAPAction']);
  curl_setopt ($session, CURLOPT_HTTPHEADER, $header_array);
  curl_setopt ($session, CURLOPT_CUSTOMREQUEST, "POST");
 }
}

// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Make the call
$xml = curl_exec($session);

// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: text/xml");

echo $xml;
curl_close($session);

?>

The reason why this request is failing is because the remote domain you are trying to make an AJAX request to doesn't send proper CORS headers. 此请求失败的原因是,您尝试向其发出AJAX请求的远程域未发送正确的CORS标头。 So the browser will block the request. 因此,浏览器将阻止该请求。 You should talk to the administrators of the bulksms.icubetech.com to explicitly enable CORS for your own domain. 您应该与bulksms.icubetech.com的管理员联系,以明确为您自己的域启用CORS。

This issue apparently has nothing to do with your front end code. 显然,此问题与您的前端代码无关。 The server you are trying to access should allow requests from other domains. 您尝试访问的服务器应允许来自其他域的请求。 In your case bulksms.icubetech.com does not allow CORS request from domain www.w3schools.com. 在您的情况下,bulksms.icubetech.com不允许来自域名www.w3schools.com的CORS请求。

暂无
暂无

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

相关问题 Javascript-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource XHR无法加载 <URL> 。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XHR cannot load <URL>. 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 角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs 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 请求的资源 .htaccess 上不存在 Access-Control-Allow-Origin 标头 - No Access-Control-Allow-Origin header is present on the requested resource .htaccess XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - Access-Control-Allow-Origin header is not present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - No Access-Control-Allow-Origin header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM